Just out of curiosity: why does C ++ choose a = new A instead of a = A.new as a way to instantiate an object? Doesn't the latter seem more like an object oriented one?
It? It depends on how you define "object oriented."
If you defined it, then how Java did it, because "everything should have the syntax of the form" XY ", where X is the object and Y is what you want to do with this object, then yes, you are right. This not object-oriented, but Java is the pinnacle of OOP programming.
But, fortunately, there are several people who believe that the “object-oriented” should relate to the behavior of your objects, and not to the syntax. Essentially, this should be reduced to what Wikipedia says:
Object-oriented programming is a programming paradigm that uses "objects" - data structures consisting of data fields and methods, together with their interactions - to develop applications and computer programs. Programming methods may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance
Note that it does not say anything about syntax. It does not say, "and you must call each function, specifying the name of the object, followed by a period, followed by the name of the function."
And given this definition, foo(x) is just as object oriented as x.foo() . All that matters is that X is an object, i.e. It consists of data fields and a set of methods by which it can be manipulated. In this case, foo is obviously one of those methods, no matter where it is defined, and no matter what syntax is used to call it.
C ++ gurus have long understood this, and articles like this . An object’s interface is not only a set of member methods (which can be called using point syntax). This is a set of functions that can manipulate an object. Whether they are members or friends does not really matter. It is object-oriented as long as the object can remain consistent, that is, it is able to prevent the random use of arbitrary functions.
So, why is A.new being more object oriented? How will this form give you the “best” objects?
One of the key goals of OOP was to allow the use of reusable code.
If new was a member of each class, this would mean that each class had to define its own new operation. If it is not a member, each class can reuse the same one. Since the functionality is the same (allocate memory, call constructor), why not put it open, where all classes can reuse it? (Preemptive nitpick: Of course, the same new implementation could be reused in this case, inheriting from some common base class or just from compiler magic. But ultimately, why bother when we can just put the mechanism outside the class first of all)