Create Object: A. New or New A?

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?

+6
c ++ language-design instance
source share
8 answers

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)

+12
source share

. in C ++, it is used only to access a member, so the right side of a point is always an object, not a type. If something would be more logical to do A::new() than A.new() .

In any case, the dynamic allocation of objects is special, since the compiler allocates memory and creates the object in two stages and adds code to eliminate exceptions at any stage, ensuring that memory never leaks out. To make it look like a call to a member function, rather than a special operation, it can be considered to hide the special nature of the operation.

+11
source share

I think the biggest confusion here is that the new one has two meanings: there is a built-in new expression (which combines memory allocation and object creation), and then there is an overloaded operator new (which works only with memory allocation). The first, as far as I can see, is whose behavior you cannot change, and therefore it makes no sense to mask it as a member function. (Or it should be - or look like - a member function that no class can implement / override !!)

This will also lead to another inconsistency:

  int* p = int.new; 

C ++ is not a pure OOP language, since not all of this is an object.

C ++ also allows the use of free functions (which is encouraged by some authors and the example set in the design of SC ++ L), with which a C ++ programmer should be comfortable. Of course, the new expression is not a function, but I don’t see how a syntax vaguely reminiscent of a call to a free function can disable someone in a language where function calls are often found.

+3
source share

read the code (it works) and then you will have different ideas:

 CObject *p = (CObject*)malloc(sizeof *p); ... p = new(p) CObject; p->DoSomthing(); ... 
+2
source share

A.new is a static function of A , and a = new A allocates memory and calls the constructor of the object after

+1
source share

In fact, you can create an object with something like A.new if you add the correct method:

 class A{ public: static A* instance() { return new A(); } }; A *a = A::instance(); 

But this is not so. The syntax is wrong: you can distinguish between :: and . "operations", exploring its right side.

I think the reason is memory management. In C ++, unlike many other object-oriented languages, memory management is performed by the user. There is no default garbage collector there, although it includes standard and non-standard libraries, as well as various memory management methods. Therefore, the programmer must see the new operator in order to understand that memory allocation has been allocated here!

If it was not overloaded, using the new operator first allocates raw memory, and then calls the constructor of the object, which creates it in the allocated memory. Since a raw low-level operation is used here, it must be a separate language operator, and not just one of the class methods.

+1
source share

I believe that there is no reason. Its a = new a only because it was first designed in this way. Looking back, there should probably be a = a.new ();

0
source share

Why should everyone have a separate new class?

I do not think that it is needed at all, because the goal of the new one is to allocate the appropriate memory and build the object by calling the constructor. Thus, the behavior of the new is unique and independent of any class. So why not make resuable?

You can redefine the new one when you want to manage the memory yourself (that is, allocating the memory pool once and returning the memory on demand).

0
source share

All Articles