From 5217923366964914708
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,3cdb654b702f7476
X-Google-Attributes: gidf78e5,public
From: llewelly.@@edevnull.dot.com
Subject: Re: virtual functions
Date: 2000/08/16
Message-ID: <m23dk5jiuq.fsf@brownie.frogger.foobar.snot>#1/1
X-Deja-AN: 658982472
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <399A78E4.8345D7B@gmx.at>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 966443150 mail2news:26002 mail2news mail2news.demon.co.uk
Organization: XMission http://www.xmission.com/
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
User-Agent: Gnus/5.0806 (Gnus v5.8.6) Emacs/20.7
Mime-Version: 1.0
NNTP-Posting-Date: 16 Aug 2000 16:01:44 GMT
Newsgroups: comp.std.c++

"Gerhard W. Gruber" <sparhawk@gmx.at> writes:

> Compiler: VisualC++ 5.0
> 
> I have a base class that defines a functions as virtual. In the
> constructor I call this function and it initializes certain members. Now
> I derived another class and there I defined the virtual function as
> well,  to override the original one.
> 
> Now I call the constructor from the derived class and it in turn calls
> the virtual function, but the problem is that the called function is not
> the one that overrides it. Instead it calls the function of the base
> class.
> 
> Maybe I missunderstand something here, but I thought that when I decalre
> a function as virtual always the overriding function is called unless I
> specify <class>::<function> explicitly.
> 
> Is this a missbehaving of the compiler or is this correct?

Your compiler is correct in this case; if a virtual function is called
  from inside a constructor, the method of that class - *not* the
  overriding method - is called.

> 
> How can I achive that a overiding function is called? The base class
> shouldn't and doesn't know about classes that overrides it but what good
> is a virtual function when it doesn't get called?
>

For the full philosophy behind this decision, see D&E 13.2.4 (pg
  282-284) 

Prior to the completion of the constructor, an object is
  uninitialized.

Most methods must assume the object has been initialized in order to
  do what they were designed to do. If you could call a typical
  overriding virtual method from a base class constructor, it would
  most likely fail horribly.

If overriding virtual methods could be called from base class
  constructors, all overriding virtual methods would have to be
  written as if the object were unitialized.

C++ chose to make ordinary virtual methods safe to write - at the cost
  of disallowing virtual initialization.

It seems to me that a virtual initialization method such as what you
  want implies a close coupling between the implementation of the base
  class's constructor and the derived class. How could the author of
  the overriding virtual initialization method know what to
  initialize without understanding the implementation of the base
  class's constructor? Such tight coupling is usually poor design, as
  it makes derived classes likely to break when the implementation of
  the base class is changed.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




