Calling virtual function in constructor

Discussion in 'C++' started by Alex Vinokur, Feb 16, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Microsoft C++ Version 13.10.3077

    Why is virtual function init() called in constructor here?

    ====== foo.cpp ======
    #include "iostream"
    using namespace std;

    class Base
    {
    public:
    virtual void init () {cout << "Base::init()" << endl; }
    Base() { cout << "Base::Ctor()" << endl;}
    };

    class Derived : public Base
    {
    public:
    void foo() { cout << "Derived::foo()" << endl; }
    virtual void init () { cout << "Derived::init()" << endl; foo ();
    }
    Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
    };

    int main ()
    {
    Base * pb = new Derived;
    return 0;
    }

    =====================


    ====== Run ======

    Base::Ctor()
    Derived::Ctor()
    Derived::init() // Why not Base::init()
    Derived::foo()

    ================


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Feb 16, 2006
    #1
    1. Advertising

  2. Alex Vinokur

    Sharad Kala Guest

    "Alex Vinokur" <> wrote in message
    | Microsoft C++ Version 13.10.3077
    |
    | Why is virtual function init() called in constructor here?

    You can call virtual functions inside constructor, but the call is resolved
    statically.

    [snip]

    | ====== Run ======
    |
    | Base::Ctor()
    | Derived::Ctor()
    | Derived::init() // Why not Base::init()

    Why should it be Base::init ?

    | Derived::foo()

    Sharad
    Sharad Kala, Feb 16, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Rolf Magnus Guest

    Alex Vinokur wrote:

    > Microsoft C++ Version 13.10.3077
    >
    > Why is virtual function init() called in constructor here?


    Why not?

    > ====== foo.cpp ======
    > #include "iostream"


    Should be:

    #include <iostream>

    > using namespace std;
    >
    > class Base
    > {
    > public:
    > virtual void init () {cout << "Base::init()" << endl; }
    > Base() { cout << "Base::Ctor()" << endl;}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > void foo() { cout << "Derived::foo()" << endl; }
    > virtual void init () { cout << "Derived::init()" << endl; foo ();
    > }
    > Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
    > };
    >
    > int main ()
    > {
    > Base * pb = new Derived;
    > return 0;
    > }
    >
    > =====================
    >
    >
    > ====== Run ======
    >
    > Base::Ctor()
    > Derived::Ctor()
    > Derived::init() // Why not Base::init()


    Because Derived::init() overrides it.

    > Derived::foo()
    Rolf Magnus, Feb 16, 2006
    #3
  4. Alex Vinokur

    Alex Vinokur Guest

    Rolf Magnus wrote:
    > Alex Vinokur wrote:

    [snip]
    > >
    > > Why is virtual function init() called in constructor here?


    Should be:

    Why is virtual function Derived::init() (not Base::init()) called in
    constructor here?

    >
    > Why not?
    >
    > > ====== foo.cpp ======
    > > #include "iostream"

    >
    > Should be:
    >
    > #include <iostream>

    Of course. Thanks.

    [snip]

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Feb 16, 2006
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    Sharad Kala wrote:
    > "Alex Vinokur" <> wrote in message
    > | Microsoft C++ Version 13.10.3077
    > |
    > | Why is virtual function init() called in constructor here?
    >
    > You can call virtual functions inside constructor, but the call is resolved
    > statically.


    When can it cause problem?

    The program I sent is not an example of such a problem (!?)

    >
    > [snip]
    >
    > | ====== Run ======
    > |
    > | Base::Ctor()
    > | Derived::Ctor()
    > | Derived::init() // Why not Base::init()
    >
    > Why should it be Base::init ?
    >
    > | Derived::foo()
    >
    > Sharad


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Feb 16, 2006
    #5
  6. Alex Vinokur

    Rolf Magnus Guest

    Sharad Kala wrote:

    > | Why is virtual function init() called in constructor here?
    >
    > You can call virtual functions inside constructor, but the call is
    > resolved statically.


    That's actually not true. Well, if you call it directly from the
    constructor, the effect is more or less the same. But if you go indirect
    (like through another function call), you can see that polymorphism is
    already active in the constructor. You just have to remember that during
    exeuction of the derived class's constructor, the object has obviously only
    been constructed up to that class. Therefore its dynamic type is
    temporarily that class. See this example:

    #include <iostream>

    class Base
    {
    public:
    void init()
    {
    do_init();
    }
    virtual void do_init()
    {
    std::cout << "Base::do_init()\n";
    }
    };

    class Derived : public Base
    {
    public:
    Derived() { init(); }

    void do_init()
    {
    std::cout << "Derived::do_init()\n";
    }
    };

    class EvenMoreDerived : public Derived
    {
    public:
    void do_init()
    {
    std::cout << "EvenMoreDerived::do_init()\n";
    }
    };

    int main()
    {
    EvenMoreDerived();
    }

    Since init() is called from Derived's constructor, Derived's implementation
    of do_init() gets called, even though init() is a member function of Base.
    Rolf Magnus, Feb 16, 2006
    #6
  7. Alex Vinokur

    Rolf Magnus Guest

    Alex Vinokur wrote:

    >> > Why is virtual function init() called in constructor here?

    >
    > Should be:
    >
    > Why is virtual function Derived::init() (not Base::init()) called in
    > constructor here?


    As I wrote, because Derived::init() overrides Base::init().
    Rolf Magnus, Feb 16, 2006
    #7
  8. Alex Vinokur

    Sharad Kala Guest

    "Rolf Magnus" <> wrote in message | Sharad Kala wrote:
    |
    | > | Why is virtual function init() called in constructor here?
    | >
    | > You can call virtual functions inside constructor, but the call is
    | > resolved statically.
    |
    | That's actually not true. Well, if you call it directly from the
    | constructor, the effect is more or less the same. But if you go indirect
    | (like through another function call), you can see that polymorphism is
    | already active in the constructor.

    Oh yeah..thanks for pointing it out.

    Sharad
    Sharad Kala, Feb 16, 2006
    #8
  9. Alex Vinokur

    Alex Vinokur Guest

    "Alex Vinokur" <> wrote in message news:...

    [snip]
    >
    > ====== foo.cpp ======
    > #include "iostream"
    > using namespace std;
    >
    > class Base
    > {
    > public:
    > virtual void init () {cout << "Base::init()" << endl; }
    > Base() { cout << "Base::Ctor()" << endl;}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > void foo() { cout << "Derived::foo()" << endl; }
    > virtual void init () { cout << "Derived::init()" << endl; foo ();
    > }
    > Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
    > };
    >
    > int main ()
    > {
    > Base * pb = new Derived;
    > return 0;
    > }
    >
    > =====================
    >
    >
    > ====== Run ======
    >
    > Base::Ctor()
    > Derived::Ctor()
    > Derived::init() // Why not Base::init()
    > Derived::foo()
    >
    > =================


    [snip]

    The example above doesn't depict the problem.

    Here is an example that depicts the problem and contains an answer.

    ====== foo2.cpp ======
    #include "iostream"
    using namespace std;

    class Base
    {
    public:
    virtual void init ()
    {
    cout << "Base::init()" << endl;
    }
    Base()
    {
    cout << "Base::ctor()" << endl;
    init (); // only Base::init() is called
    }
    void foo()
    {
    cout << "Base::foo()" << endl;
    init (); // virtual init() of a _relevant_ class is called, i.e., Base::init() or Derived::::init()
    }
    };


    class Derived : public Base
    {
    public:
    void init ()
    {
    cout << "Derived::init()" << endl;
    }
    Derived () : Base()
    {
    cout << "Derived::ctor()" << endl;
    }
    };


    int main ()
    {
    Base * pb = new Derived;

    cout << endl;
    pb->foo();

    return 0;

    }
    ======================


    ====== Run ======

    Base::ctor()
    Base::init() // Base::ctor() calls Base::init() because Derived-object doesn't exist here
    Derived::ctor()

    Base::foo()
    Derived::init() // Base::foo() calls Derived::init() because Derived-object exists here

    =================


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Feb 16, 2006
    #9
    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. Andreas Lagemann
    Replies:
    8
    Views:
    451
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    423
  3. Replies:
    11
    Views:
    675
    James Kanze
    Sep 10, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,183
  5. Yakov Gerlovin
    Replies:
    2
    Views:
    275
    Yakov Gerlovin
    Oct 11, 2011
Loading...

Share This Page