In C ++, when creating an object, is the new one used implicitly?

When I create a class object let's say

class A { public: A() {} }; A a; 

Is only the constructor called? Or is the new operator used implicitly?

How should we do A* b = new A();

Also, where will a and b be stored in memory? Stack or heap?

+6
source share
2 answers

In the first case, if a not a global variable, then it will be pushed onto the stack, and b will be pushed onto the heap.

And in the first case, only the constructor is called. new never called, unless you do it explicitly, as in the second case.

+8
source

No new is called implicitly. new returns a pointer to the type of the created object, while the constructor call does not have a return type. Objects created with new will exist on the heap. The new one will allocate memory, and then call the constructor. Objects created in form A a will exist on the stack (unless they are global variables).

+6
source

Source: https://habr.com/ru/post/923312/


All Articles