2

I am confused with the following statement.

From the specification:

Before the implicitly-declared default constructor for a class is implicitly defined, all the implictly-declared default constructors for its base classes and its non static data members shall have been implicitly defined.

What i understand is :

implicitly declared default constructor is implicitly defined when the object is created.

What does the above statement means?. if base class contains explicit default constructor, then the derived class can not have a implicit default constructor?. Kindly clarify, it could be nice if someone provides piece of sample code.

1
  • If you're familiar with American TV shows, it's similar to the familar statement from police: "..You have the right to an attorney. If you can't afford one, one will be provided...". If you already have one, you don't get a second one. Commented Jul 20, 2012 at 13:46

2 Answers 2

4

The statement means that when the compiler must provide a definition for an implicitly declared default constructor (i.e. when such constructor is odr-used), before the definition of the current constructor the compiler must ensure that all members can be default constructed, and for that it might need to implicitly define any implicitly declared default constructor of the members.

For an example:

struct A { int x; };    
struct B { A a; };      // [1]
int main() {
   B b;                 // [2]
}

The definition of the variable b in [2] is an odr-use of the implicitly declared constructor for B, but before the compiler implicitly defines B::B(), because it has a member of type A declared in [1], it needs to implicitly define A::A(). The reason being that B::B() will odr-use A::A() in it's initialization list.

Sign up to request clarification or add additional context in comments.

2 Comments

So, if i understand correctly, 1) definition happens when the object is created, 2) it makes sure of all its members have implicitly defined default constructors, and its base classes?. am i correct?
Nice example. Upvoting for it. :)
2

Assume you have classes Base and Derived (which is derived from base).

Let's assume both of them have implicitly declared default constructors. When you will create an object of Derived class, the following will happen. First, implicitly-declared default constructor for the Base class will be defined. After that, the same will happen with the implicitly-declared constructor for the Derived class.

This totally makes sense, because when you create the object of Derived class, the constructor for the Base class gets called first. If it will not be defined by that time, well, something bad will probably happen.

The same things apply to any class members that have such constructors: those are defined before the class' own constructor will get defined.

4 Comments

what if the base class contains explicitly defined default constructor?. does it nothing?
@Whoami, if there is an explicitly defined and declared default constructor for Base (let's call it "real" for shortness), then we don't need the implicitly-declared one. So the implicitly-declared constructor for Derived will be defined, and then the object will be created using the "real" constructor from the base class and the implicitly-declared one from the derived class.
@Whoami, basically, the idea is "by the time a constructor gets called, it should exist". So if you've implemented a constructor, it will be used. If not, the compiler will generate a default constructor and then use it.
The rule also, indirectly, says that if the base class constructor cannot be implicitly defined (because it contains something needing an explicit initialization), the derived class' default constructor will not be implicitly defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.