473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Calling virtual method within the constructor

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.
Jun 27 '08 #1
7 1977
On Jun 8, 2:01 am, Fernando Gómez <fernando.a.gome...@gmail.com>
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.
Jun 27 '08 #2
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.
Jun 27 '08 #3
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.

Jun 27 '08 #4
On Jun 8, 2:04 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
Jun 27 '08 #5
On Jun 8, 2:05 am, Rolf Magnus <ramag...@t-online.dewrote:
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.

Jun 27 '08 #6
Fernando Gómez wrote:
On Jun 8, 2:05 am, Rolf Magnus <ramag...@t-online.dewrote:
>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
Jun 27 '08 #7
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."
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
7
by: vsgdp | last post by:
I have an abstract class A: class A { public: A(){ f(); } virtual void f() = 0; }; class B : public A
8
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. ...
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.