This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++ - Calling virtual function from constructor
- From: Frank Schafer <frank dot schafer at setuza dot cz>
- To: Eddy Ilg <eddy at fericom dot net>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 23 Nov 2001 09:41:51 +0100
- Subject: Re: C++ - Calling virtual function from constructor
- Organization: SETUZA a.s.
- References: <000c01c1735c$33131a80$6300a8c0@Freedom>
- Reply-to: frank dot schafer at setuza dot cz
Eddy Ilg wrote:
> Hi,
>
> I have a question that regards C++. In my program I have a class with some
> virtual functions. One virtual Function does some initialization specific to
> the derived class. It looks someThing like this:
>
> class scmObject
> {
> protected:
> virtual void construct()=0;
> public:
> scmObject();
> }
>
> scmObject::scmObject()
> {
> ...
> construct();
> }
>
> When I try to compile this I get:
> scmObject.cpp: In method `scmObject::scmObject(BRANCH *)':
> scmObject.cpp:50: abstract virtual `void scmObject::construct(BRANCH *)'
> called from constructor
>
> When I chage it like this I can compile it:
>
> class scmObject
> {
> protected:
> virtual void construct()=0;
> void init();
> public:
> scmObject();
> }
>
> scmObject::scmObject()
> {
> ...
> init();
> }
>
> void scmObject::init()
> {
> construct();
> }
>
> Why can I not or how could I call a virtual function from the contstructor?
>
> Thanks
>
> Eddy
Hi,
seems you found the reason yoyrself ;-) ... and the early stage fault is ... (
see the C++ standard ) ... you CAN call any virtual function, but you CAN'T
call an abstract function ( the ...=0 ) from the constructor.
Regards
Frank
begin:vcard
n:Schafer;Frank
x-mozilla-html:FALSE
org:SETUZA a.s.;IT
adr:;;;;;;
version:2.1
email;internet:frank.schafer@setuza.cz
title:Dipl. Ing.
note:System administrator
x-mozilla-cpt:;0
fn:Frank Schafer
end:vcard