0

Assuming that MyClass is a custom class with a custom default constructor, and that this is in C++... What is the difference between these 3 ways of initializing a MyClass object?

MyClass myObj;
MyClass* myObj = new MyClass;
MyClass* myObj = new MyClass();

What I already know: I know that the first object exists solely in the stack, whereas the following two objects are allocated on heap memory. I know that the two last objects are stored in pointer variables, and so calling functions would require using "->" instead of the dot operator.

Are there any other differences?

erickcgt
  • 1
  • 1
  • There's no difference between `new MyClass` and `new MyClass()` – Barmar Oct 23 '20 at 22:43
  • @barmar: there may be a difference – Eugene Oct 23 '20 at 22:44
  • are there any additional differences between the 1st initialization and the other two, aside from the ones i mentioned? – erickcgt Oct 23 '20 at 22:44
  • Please read the dupe. If you have a specific question that is not addressed there, please edit the post. – cigien Oct 23 '20 at 22:44
  • My answer before the close happened: As you said, the first line allocates memory on the stack, and the next two allocate memory on the heap. However, now the behavior diverges based upon whether MyClass is a trivial class (that is, all public data members, each of which is either a primitive type or another trivial type.) If it is not, both of the last two lines attempt to invoke the MyClass constructor which takes no arguments. However, if MyClass is a trivial type, then the second line allocates memory for MyClass but does not initialize it, like malloc would for a C struct. – Anonymous1847 Oct 23 '20 at 22:45
  • Whereas, the third line would *value-initialize* the new instance, which in this case means make it all zero, i.e. like calloc in C. – Anonymous1847 Oct 23 '20 at 22:45
  • Yes, the duplicate doesn't seem quite correct. Unfortunately I don't think I can vote to reopen yet... – Anonymous1847 Oct 23 '20 at 22:47
  • Okay, and as a final question, the first initialization (MyClass myObj;) calls on the constructor as well, correct? – erickcgt Oct 23 '20 at 22:48
  • @Anonymous1847 If you think the dupe is wrong, you can flag/vote to reopen. You can also notify the user who closed the post. Also, it's not at all clear to me that the OP is asking about that. Added a dupe for that anyway :) – cigien Oct 23 '20 at 22:48
  • @erickcgt Sorry, my terminology maybe was a bit off. The type property is not being "trivial," it's being "POD" (plain-old data), so just think C struct without any C++ frills like user-defined copy constructors. Primitive types other than references are also POD. Lines 1 and 2 do default initialization, line 3 does value-initialization. Default initialization for non-POD types calls the constructor. Default-initialization for POD types does nothing (makes them uninitialized). – Anonymous1847 Oct 23 '20 at 22:59
  • Value-initialization always calls the constructor for non-POD or zeroes for POD. EDIT: Looking it up again, it seems like some of the behavior changed in C++11, particularly default-initialization now calls the constructor even if class type is POD. Default-initialization for primitive types still leaves them uninitialized. It's confusing, I know. EDIT 2: To recap, rest assured if you have a class type, its default constructor will always be called if it's non-POD pre-C++11, and *always* after. – Anonymous1847 Oct 23 '20 at 22:59

0 Answers0