C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

virtual member function in constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Karsten Hochkirch
Guest





PostPosted: Fri Jan 30, 2004 8:21 pm    Post subject: virtual member function in constructor Reply with quote



Hi,

I have just encountered a problem when calling a virtual member function
in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.

I have attached a small example.

Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?

Thanks

Karsten



#include <iostream>
using namespace std;
class A {
public:

virtual A(){init();}
virtual ~A(){}
virtual void init(){
cout << "class A::initn";
}
void test(){init();}

};

class B: public A{
public:
B():A(){}
virtual ~B(){}
virtual void init(){
cout << "class B::initn";
}
};

int main(int,char**){


// only class A's init is called
B b;

// works as I expected
b.test();
return 0;

}


Back to top
Karsten Hochkirch
Guest





PostPosted: Fri Jan 30, 2004 8:23 pm    Post subject: Re: virtual member function in constructor Reply with quote



Oops,

the 'virtual' in front of the constructor was just desparate try I
forgot to remove before attaching the file...

Karsten Hochkirch wrote:

Quote:
Hi,

I have just encountered a problem when calling a virtual member
function in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.

I have attached a small example.

Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?

Thanks

Karsten

------------------------------------------------------------------------



#include using namespace std;
class A {
public:

virtual A(){init();}
virtual ~A(){}
virtual void init(){
cout << "class A::initn";
}
void test(){init();}

};

class B: public A{
public:
B():A(){}
virtual ~B(){}
virtual void init(){
cout << "class B::initn";
}
};

int main(int,char**){


// only class A's init is called
B b;

// works as I expected
b.test();
return 0;

}





Back to top
Ron Natalie
Guest





PostPosted: Fri Jan 30, 2004 8:31 pm    Post subject: Re: virtual member function in constructor Reply with quote




"Karsten Hochkirch" <K.Hochkirch (AT) vbew (DOT) net> wrote


Quote:

Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?

That is the way it is supposed to work. For the duration of the execution of the
the constructor body, the dynamic type of the object is considered to be that of
the constructor that is running. The primary reason is that the derived class has
yet to be initialized (the base class constructor is run first), so you don't want to
be roaming around in the derived code.

The work around is to place any virtual function based common initialization in
a base class member function other than the constructor and then call it from
the derived constructor.

The same thing happens for destructors (just in the reverse order).


Back to top
Le Géant Vert
Guest





PostPosted: Fri Jan 30, 2004 8:37 pm    Post subject: Re: virtual member function in constructor Reply with quote

Karsten Hochkirch wrote:

Quote:
Oops,

the 'virtual' in front of the constructor was just desparate try I
forgot to remove before attaching the file...

Karsten Hochkirch wrote:

Hi,

I have just encountered a problem when calling a virtual member
function in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.

I have attached a small example.

Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?

Thanks

Karsten

this is not really a feature, this is the way things are supposed to

happen... When the constructor's called, the object does not exist yet :
so the code of your overloaded virtual method is unknown and unreachable
; thus, only the one of the parent class can be called.
(a funny thing you can do once you've understood this, is to use it to
call code of a pure virtual method)
the tip I'd told you is to never call any method in a constructor, and
if needed, to provided an init() that should be called by the programmer
(not as you're doing in your code sample, where init() is called from
the ctor)

Back to top
Jonathan Turkanis
Guest





PostPosted: Fri Jan 30, 2004 8:40 pm    Post subject: Re: virtual member function in constructor Reply with quote


"Karsten Hochkirch" <K.Hochkirch (AT) vbew (DOT) net> wrote

Quote:
Hi,

I have just encountered a problem when calling a virtual member
function
in a constructor.

Within a constructor, virtual calls are resolved using the vtable of
the class in which the constructor is defined, rather than the vtable
of the fully derived type of the object being constructed. Therefore,
you must be very careful when calling virtual functions from
constructors; in particular, the possibility of calling a pure virtual
function exists.

Jonathan



Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jan 30, 2004 8:55 pm    Post subject: Re: virtual member function in constructor Reply with quote

On Fri, 30 Jan 2004 15:31:10 -0500, "Ron Natalie" <ron (AT) sensor (DOT) com> wrote:

Quote:

"Karsten Hochkirch" <K.Hochkirch (AT) vbew (DOT) net> wrote



Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?


[Correct explanation of why, snipped.]


See also the FAQ:
<url: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.3>



Quote:
The work around

Actually there are many different workarounds.

The most common workarounds are also in the FAQ (I'm happy to take much credit
for their inclusion, although credit for the text goes to Marshall Cline):

<url: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4>



Quote:
is to place any virtual function based common initialization in
a base class member function other than the constructor and then call it from
the derived constructor.

This only seems to allow virtuality for one level of class derivation.

It's not a commonly used workaround.

I don't recommend it, but perhaps it should be included among the others in
the FAQ?


Back to top
David Harmon
Guest





PostPosted: Fri Jan 30, 2004 8:56 pm    Post subject: Re: virtual member function in constructor Reply with quote

On Fri, 30 Jan 2004 21:21:34 +0100 in comp.lang.c++, Karsten Hochkirch
<K.Hochkirch (AT) vbew (DOT) net> was alleged to have written:
Quote:
I have just encountered a problem when calling a virtual member function
in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[23.3] When my base class's constructor calls a virtual function on its
this object, why doesn't my derived class's override of that virtual
function get invoked?" It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/



Back to top
Karsten Hochkirch
Guest





PostPosted: Fri Jan 30, 2004 11:08 pm    Post subject: Re: virtual member function in constructor Reply with quote

Ok, I think I understand by now.


Thanks to all of you for the anwers!!


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.