Calling virtual method within the constructor

  • Thread starter Fernando Gómez
  • Start date
F

Fernando Gómez

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.
 
F

Fernando Gómez

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.
 
I

Ian Collins

Fernando said:
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.
 
R

Rolf Magnus

Fernando said:
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.
 
F

Fernando Gómez

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.
 
F

Fernando Gómez

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.
 
K

Kai-Uwe Bux

Fernando said:
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
 
R

Rolf Magnus

Fernando said:
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."
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top