Constructor question, how does the call to the parent constructor work?

M

marcwentink

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?
 
J

Jim Langston

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) {}
}
 
M

marcwentink

Jim

[some things on the result of constructor calling]

Thanks I understand that part now. But my remaining question is: does
this mean that the constructor actually is a virtual function, and that
the parent constructor is found by the VT-table? And if this is true,
then we never actually use the word virtual in declaring a constructor,
but it in fact is a virtual method. You would just not use the virtual
keyword, since all constructors have to be virtual anyway.

Or am I way off now?
 
M

Marcin Kalicinski

Thanks I understand that part now. But my remaining question is: does
this mean that the constructor actually is a virtual function, and that
the parent constructor is found by the VT-table?

Why do you think it needs a virtual table? When calling constructor of base
class, compiler has all the information needed, and makes the decision what
to call in compile time. There's no need for any runtime mechanisms like
virtual functions.

Marcin
 
M

marcwentink

Why do you think it needs a virtual table?
When calling constructor of base class, compiler has all the information needed

You're very right! And it would take up space even, and be inefficient.

Actually, and that is entirely off topic...., I am confused by the
concept of a virtual constructor in a language like Delphi/ObjectPascal
were they do have a virtual constructor. Now I wonder why Delphi does
have a virtual constructor. What the use, since you would know
information compile time on creation of an object of a certain type?
 
J

Jack Klein

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()

[snip]

Of course the line above makes the program ill-formed and no longer
C++.
 
R

raxitsheth

I think you want
Opensource is good to learn sample implementation detail.

Raxit Sheth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top