Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling virtual method within the constructor

Reply
Thread Tools

Calling virtual method within the constructor

 
 
Fernando Gómez
Guest
Posts: n/a
 
      06-08-2008
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.
 
Reply With Quote
 
 
 
 
Fernando Gómez
Guest
Posts: n/a
 
      06-08-2008
On Jun 8, 2:01 am, Fernando Gómez <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-08-2008
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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-08-2008
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.

 
Reply With Quote
 
Fernando Gómez
Guest
Posts: n/a
 
      06-08-2008
On Jun 8, 2:04 am, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Fernando Gómez
Guest
Posts: n/a
 
      06-08-2008
On Jun 8, 2:05 am, Rolf Magnus <(E-Mail Removed)> 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.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-08-2008
Fernando Gómez wrote:

> On Jun 8, 2:05 am, Rolf Magnus <(E-Mail Removed)> 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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-08-2008
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."


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
How to call a constructor from within another constructor Andy C++ 9 09-24-2006 03:17 PM
I see no difference in an inheirited non-virtual method and an inheirited virtual method jlopes C++ 7 11-19-2004 07:47 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments