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

Discussion in 'C++' started by marcwentink@hotmail.com, May 8, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > 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) {}
    }
    Jim Langston, May 8, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. > 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
    Marcin Kalicinski, May 8, 2006
    #4
  5. Guest

    > 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, 2006
    #5
  6. Jack Klein Guest

    On Mon, 8 May 2006 05:53:18 -0700, "Jim Langston"
    <> wrote in comp.lang.c++:

    > <> wrote in message
    > news:...
    > > 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, May 9, 2006
    #6
  7. Guest

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

    Raxit Sheth
    , May 9, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy
    Replies:
    2
    Views:
    3,115
  2. xenophon

    Call Parent Constructor?

    xenophon, Feb 15, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    513
  3. pantalaimon
    Replies:
    3
    Views:
    51,064
    John Harrison
    Oct 9, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,230
  5. Warren Tang
    Replies:
    9
    Views:
    670
    James Kanze
    Apr 15, 2008
Loading...

Share This Page