class A : public B
{
...
}
// case I : explicitly call the base class default constructor
A::A() : B()
{
...
}
// case II : don't call the base class default constructor
A::A() // : B()
{
...
}
Is the case II equal to case I?
To me, I assume that the default constructor of base class B is NOT called in case II. However, despite still holding this assumption, I have run a test which proves otherwise:
class B
{
public:
B()
{
cout << "B constructor" << endl;
}
};
class A : public B
{
public:
A()
{
cout << "A constructor" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
// output from VS2008
B constructor
A constructor
Press any key to continue . . .
An initialization list can be used to explicitly call a constructor that takes arguments for a data member that is an object of another class (see the employee constructor example above). In a derived class constructor, an initialization list can be used to explicitly call a base class constructor that takes arguments.
For multiple inheritance order of constructor call is, the base class's constructors are called in the order of inheritance and then the derived class's constructor.
If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base.
The data members j and k , as well as the base class A must be initialized in the initializer list of the constructor of B . You can use data members when initializing members of a class.
The base class constructor is called in both cases.
Here is a link to an article with more info.
If the base class constructor doesn't take any argument, then explicit mention of it in the initialization list is not needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With