r/learncpp • u/Niket_N1ghtWing • May 08 '22
Does new behave differently when creating a variable versus when instantiating an object on the heap memory?
I am confused about how new is implemented in following scenarios:
#1
ClassName* ptr1 = new ClassName(10); // Assuming there is a constructor that requires an int
#2
int* ptr2 = new int;
Does the keyword after new behaves differently in the two cases? i.e. in #1, ClassName(10) calls and passes the argument to the constructor, whereas in #2, the int after new is supposed to define the pointer type that new will produce?
7
Upvotes
View all comments
Show parent comment
1
u/flyingron May 08 '22
Even classes don’t necessarily have tables. New does give a hoot about tables anyhow. Vtables if needed are created at object creation time no matter whether the object is dynamically allocated, statically allocated, or automatically created.