|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jun 8, 2:04 am, Ian Collins <ian-n...@hotmail.com> 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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jun 8, 2:05 am, Rolf Magnus <ramag...@t-online.de> 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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Fernando Gómez wrote:
> On Jun 8, 2:05 am, Rolf Magnus <ramag...@t-online.de> 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
* Fernando Gómez:
> On Jun 8, 2:05 am, Rolf Magnus <ramag...@t-online.de> wrote: >> 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. This is not a Google Groups group, it's a Usenet group, not maintained by Google, and not existing on Google's servers. A Usenet group has no "main page"; it has no pages at all. However, since you're using Google's services, perhaps you might make the effort of typing in, say, "C++ FAQ" in Google's search engine. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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." |
|
![]() |
| Outils de la discussion | |
|
|