Question

I'm trying to understand how the compiler's default constructor works. I made this example:

#include <iostream>

class Base {
    public:
    int number;
};

class Test1 : public Base {  
};

class Test2 {
    public:
    Base base;
};

int main() {
    Test1 test1;
    Test2 test2;
    std::cout<<test1.number<<std::endl;
    std::cout<<test2.base.number<<std::endl;
}

The output of this test program is, for test1 0, and for test2 is a uninitialized (random) number. Now my question is: why in the first case (test1) the compiler's default constructor initialize number to 0 but for test2 it doesn't?

Edit: According to answers both makes undefined behavior. So, in this program, what the compiler's default constructor does?

Was it helpful?

Solution

The value of an uninitialized object is indeterminate as per 8.5/12:

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.17).

Unfortunately for you, int falls into the category of "non-default-initialized types" as per §8.5/7:

To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) for T is called (and the initialization is ill-formed if T has no default constructor or overload resolution (13.3) results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

OTHER TIPS

Actually in both cases number is non-default-initialized, meaning its value could be anything. It just so happened that test1 output 0 instead of some random number.

Personally, I can never remember what the implicitly defined default constructor does except in the simplest cases. So I only rely on it for the simplest cases.

I took a look at the C++11 January working paper (i.e., an edited version of the Standard published after the Standard was approved). Section 12.1 paragraph 6 (p. 243) states, "The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer and an empty compound-statement."

If I understand "ctor-initializer" and "compound-statement" correctly, your implicitly defined constructors are:

Test1::Test1() {}

Test2::Test2() {}

Base::Base() {}

So, in both Test1 and Test2, Base::number is never initialized (Base is, in Test2, but Base's implicit constructor doesn't initialize number).

At least, the Standard doesn't require that Base::number be initialized. Compilers are free to define things that the Standard doesn't define. The fact that you see a 0 doesn't mean the operation is defined according to the Standard, it simply means your compiler zeroed things out in this particular case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top