By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

May 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
<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) {}
}

May 8 '06 #2

P: n/a
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?

May 8 '06 #3

P: n/a
> 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


May 8 '06 #4

P: n/a
> 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?

May 8 '06 #5

P: n/a
On Mon, 8 May 2006 05:53:18 -0700, "Jim Langston"
<ta*******@rocketmail.com> wrote in comp.lang.c++:
<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()


[snip]

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 10 '06 #6

P: n/a

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

Raxit Sheth

May 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.