<ma*********@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Say I have class A that inherits from class B, and I call class A his
constructor. Then somehow class B his constructor is called also. How
does this work? Is a constructor under the hood a sort of virtual
method by default, and are the ancestors constructors called in a sort
of VT table?
A base's constructor will be called whenever a derived class's constructor
is called.
That is:
class Base
{
public:
Base() { std::cout << "Base Constructor" << std::endl; }
}
class Derived: Base
{
public:
Derived() { std::cout << "Derived Constructor" << std::endl; }
}
void main()
{
Derived MyDerived; // At this point Base Constructor is called and
Derived Constructor
}
If a Base does not have a default constructor, you'll need to supply it's
paramters in the derived initialization list.
class Base
{
public:
Base( int i ): i_(i) {}
}
class Derived: Base
{
public:
Derived( int i ): Base(i) {}
}