PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Calling virtual method within the constructor
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Calling virtual method within the constructor

Réponse
 
LinkBack Outils de la discussion
Vieux 08/06/2008, 08h01   #1
Fernando Gómez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 08/06/2008, 08h03   #2
Fernando Gómez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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.
  Réponse avec citation
Vieux 08/06/2008, 08h04   #3
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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.
  Réponse avec citation
Vieux 08/06/2008, 08h05   #4
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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.

  Réponse avec citation
Vieux 08/06/2008, 17h39   #5
Fernando Gómez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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.
  Réponse avec citation
Vieux 08/06/2008, 17h41   #6
Fernando Gómez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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.

  Réponse avec citation
Vieux 08/06/2008, 17h49   #7
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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
  Réponse avec citation
Vieux 08/06/2008, 18h22   #8
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

* 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?
  Réponse avec citation
Vieux 08/06/2008, 18h49   #9
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling virtual method within the constructor

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17752 seconds with 17 queries