3

A implicit default constructor has an empty body and an empty initializer list (primitive types undefined, and the default constructor is called for user defined types).

This post says

MyClass *c = new MyClass();

does indeed do a member-wise value-initialization, but what is the point of calling the default constructor when doing

MyClass c;

?

Is the implicit default constructor called, to ensure that the default constructors for user defined types (which might have non-trivial default constructors) are called?


Update

Seems that after the compiler-generated implicit default constructor is called, the object might not be consistently instantiated, i.e. primitive types undefined, and user defined types might (or might not be) in a known state depending on if the programmer provided default constructors.

Why then does the compiler generate an implicit default constructor which when called might instantiate an object in an unknown state?

Community
  • 1
  • 1
robor
  • 2,969
  • 2
  • 31
  • 48
  • Code is created at compile time. C++ is a very much static language. – Jonathon Reinhart Aug 27 '16 at 14:18
  • Are you aware that the first version creates a memory leak unless you use `delete c;` after you stop using the variable? – Marian Spanik Aug 27 '16 at 14:32
  • 1
    Your example demonstrate heap allocation vs stack allocation and has nothing to to with defaultness or implicitness of the constructor. I think you don't understand what default constructor is and what word "implicit" means in the context of implicit default constructor. You need to figure out that first. See: [cppreference --Default Constructor](http://en.cppreference.com/w/cpp/language/default_constructor) Just to make sure that we are on the same wave-length – Ivan Aksamentov - Drop Aug 27 '16 at 14:34
  • Historical reasons: in C we had structs, we didn't write constructors and the objects were 'just there' when instantiated. Then we wanted to port tons of code to C++ in a simple-yet-efficient way. Unless you've a state pattern with a meaningful start state, you probably want to write your own constructor(s). – lorro Aug 27 '16 at 14:37
  • @KerrekSB Don't you mean "*declared* implicitly"? I read that it was defined implicitly when it is odr-used. – David G Aug 27 '16 at 15:52
  • @0x499602D2: Yes, good point! – Kerrek SB Aug 27 '16 at 15:57

3 Answers3

5

The point of the implicit default constructor is the same point as of any other constructor.

Something needs to construct every instance of a given class. The class instance is not going to appear out of thin air, by itself. Something, somewhere, has the job of constructing the object.

If an explicit constructor is not declared, an implicit default constructor gets automatically defined, that default-constructs the class's superclasses, and any class members.

Here, "default-constructs" also includes the "do nothing" option, if the class member is a primitive type with no explicit constructor. So, in the end, the implicit default constructor may end up doing nothing. But it is still defined, if an explicit constructor is not specified (and if the implicit default constructor is not explicitly deleted, of course).

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
4

Whether you do this:

MyClass c;

or this:

MyClass *c = new MyClass();

You are invoking the default constructor.

You're right that the implicit default constructor does leave primitive types like int and double uninitialized. But it does initialize any other member variables, such as strings, by calling their default constructors (implicit or explicit).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    If `MyClass` does not have a user defined constructor, then `MyClass *c = new MyClass();` will value-initialize the object, and primitive members will get initialized to zero. – Benjamin Lindley Aug 27 '16 at 14:57
0

I read in Wikipedia Virtual Method Table

As such, the compiler must also generate "hidden" code in the constructor of each class to initialize a new objects' vpointer to the address of its class's vtable.

Perhaps that is one reason for having a default implicit constructor - when there is a class hierarchy with virtual methods, each instance needs the vpointer setup correctly.

robor
  • 2,969
  • 2
  • 31
  • 48