Calling virtual method within the constructor

Discussion in 'C++' started by Fernando Gómez, Jun 8, 2008.

  1. Hello all. I have this class with a virtual method and a constructor
    that calls this virtual method. A derived class overrides this virtual
    method, so I expected that when the base's constructor is called, it
    would call the derived version of the method. However, it does not, it
    calls the base's version. An example:

    class Base
    {
    public:
    Base() {
    Method();
    }
    virtual ~Base() { }

    virtual void Method(const Base& base) {
    cout << "Base::Method" << endl;
    }
    };

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

    virtual void Method() {
    cout << "Derived::Method" << endl;
    }
    };

    int main()
    {
    Derived d; // prints "Base::Method" !!!
    d.Method; // prints "Derived::Method"
    return EXIT_SUCCESS;
    }

    Am I missing something here? Is calling virtual members not allowed on
    the constructors? Would this be a bug from my compiler?

    Thanks in advance.
    Fernando Gómez, Jun 8, 2008
    #1
    1. Advertising

  2. On Jun 8, 2:01 am, Fernando Gómez <>
    wrote:
    > Hello all. I have this class with a virtual method and a constructor
    > that calls this virtual method. A derived class overrides this virtual
    > method, so I expected that when the base's constructor is called, it
    > would call the derived version of the method. However, it does not, it
    > calls the base's version. An example:
    >
    > class Base
    > {
    > public:
    > Base() {
    > Method();
    > }
    > virtual ~Base() { }
    >
    > virtual void Method(const Base& base) {
    > cout << "Base::Method" << endl;
    > }
    >
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived() : Base() { }
    >
    > virtual void Method() {
    > cout << "Derived::Method" << endl;
    > }
    >
    > };
    >
    > int main()
    > {
    > Derived d; // prints "Base::Method" !!!
    > d.Method; // prints "Derived::Method"
    > return EXIT_SUCCESS;
    >
    > }
    >
    > Am I missing something here? Is calling virtual members not allowed on
    > the constructors? Would this be a bug from my compiler?
    >
    > Thanks in advance.


    Ah damn it, sorry, a mistake there. The Base class should be:

    class Base
    {
    public:
    Base() {
    Method();
    }
    virtual ~Base() { }

    virtual void Method() {
    cout << "Base::Method" << endl;
    }

    };

    that is, Method without params. Sorry for that.
    Fernando Gómez, Jun 8, 2008
    #2
    1. Advertising

  3. Fernando Gómez

    Ian Collins Guest

    Fernando Gómez wrote:
    > Hello all. I have this class with a virtual method and a constructor
    > that calls this virtual method. A derived class overrides this virtual
    > method, so I expected that when the base's constructor is called, it
    > would call the derived version of the method. However, it does not, it
    > calls the base's version.
    >

    That is correct. Only the derived class constructor can call he derived
    class virtual methods.

    The derived class is not constructed when the base class constructor
    runs, so the derived class virtual method can not be called.

    --
    Ian Collins.
    Ian Collins, Jun 8, 2008
    #3
  4. Fernando Gómez

    Rolf Magnus Guest

    Fernando Gómez wrote:

    > Hello all. I have this class with a virtual method and a constructor
    > that calls this virtual method. A derived class overrides this virtual
    > method, so I expected that when the base's constructor is called, it
    > would call the derived version of the method. However, it does not, it
    > calls the base's version.


    Yes. When the base class constructor is executed, the derived part hasn't
    yet been created, so the object is not yet an instance of the derived
    class.
    Btw, this is a FAQ. You should have a look at the FAQ list of this
    newsgroup.
    Rolf Magnus, Jun 8, 2008
    #4
  5. On Jun 8, 2:04 am, Ian Collins <> wrote:
    > Fernando Gómez wrote:
    > > Hello all. I have this class with a virtual method and a constructor
    > > that calls this virtual method. A derived class overrides this virtual
    > > method, so I expected that when the base's constructor is called, it
    > > would call the derived version of the method. However, it does not, it
    > > calls the base's version.

    >
    > That is correct. Only the derived class constructor can call he derived
    > class virtual methods.
    >
    > The derived class is not constructed when the base class constructor
    > runs, so the derived class virtual method can not be called.


    Ah, that makes sense. Thanks for the answer.
    Fernando Gómez, Jun 8, 2008
    #5
  6. On Jun 8, 2:05 am, Rolf Magnus <> wrote:
    > Fernando Gómez wrote:
    > > Hello all. I have this class with a virtual method and a constructor
    > > that calls this virtual method. A derived class overrides this virtual
    > > method, so I expected that when the base's constructor is called, it
    > > would call the derived version of the method. However, it does not, it
    > > calls the base's version.

    >
    > Yes. When the base class constructor is executed, the derived part hasn't
    > yet been created, so the object is not yet an instance of the derived
    > class.
    > Btw, this is a FAQ. You should have a look at the FAQ list of this
    > newsgroup.


    Sorry, I don't see a FAQ. In the main page, there's only a list of
    posts and a "search google groups" text box.
    Fernando Gómez, Jun 8, 2008
    #6
  7. Fernando Gómez

    Kai-Uwe Bux Guest

    Fernando Gómez wrote:

    > On Jun 8, 2:05 am, Rolf Magnus <> wrote:
    >> Fernando Gómez wrote:
    >> > Hello all. I have this class with a virtual method and a constructor
    >> > that calls this virtual method. A derived class overrides this virtual
    >> > method, so I expected that when the base's constructor is called, it
    >> > would call the derived version of the method. However, it does not, it
    >> > calls the base's version.

    >>
    >> Yes. When the base class constructor is executed, the derived part hasn't
    >> yet been created, so the object is not yet an instance of the derived
    >> class.
    >> Btw, this is a FAQ. You should have a look at the FAQ list of this
    >> newsgroup.

    >
    > Sorry, I don't see a FAQ. In the main page, there's only a list of
    > posts and a "search google groups" text box.


    Google misled you. This is a usenet group; and it does not hae a "main page"
    as It is unrelated to Google. The FAQ is mentioned in the weekly welcome
    message. You can find it here:

    http://www.parashift.com/c -faq-lite/


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jun 8, 2008
    #7
  8. Fernando Gómez

    Rolf Magnus Guest

    Fernando Gómez wrote:

    > Sorry, I don't see a FAQ.


    Have a look at the thread directly before this one, with the subject:
    "===Welcome to comp.lang.c++! Read this first."
    Rolf Magnus, Jun 8, 2008
    #8
    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. Giulio
    Replies:
    9
    Views:
    1,025
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,318
    John Harrison
    Jun 29, 2003
  3. jlopes
    Replies:
    7
    Views:
    406
    jlopes
    Nov 19, 2004
  4. Andy
    Replies:
    9
    Views:
    493
    Pete Becker
    Sep 24, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,190
Loading...

Share This Page