From 5761594856856435481
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,3cdb654b702f7476
X-Google-Attributes: gidf78e5,public
X-Google-Thread: 109fba,3cdb654b702f7476
X-Google-Attributes: gid109fba,public
From: Anders Pytte <anders@milkweed.com>
Subject: Re: virtual functions
Date: 2000/08/16
Message-ID: <B5C01D64.93C3%anders@milkweed.com>#1/1
X-Deja-AN: 659024563
Content-transfer-encoding: 7bit
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 966450251 mail2news:426 mail2news mail2news.demon.co.uk
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
User-Agent: Microsoft-Outlook-Express-Macintosh-Edition/5.02.2022
Mime-version: 1.0
NNTP-Posting-Date: Wed, 16 Aug 2000 10:27:53 EDT
Newsgroups: comp.lang.c++,comp.std.c++

in article 399A78E4.8345D7B@gmx.at, Gerhard W. Gruber at sparhawk@gmx.at
wrote on 8/16/00 9:03 AM:

> 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?
> 
> 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?

The compiler will not allow you to call a non-static member function of an
object that has not been constructed. Disappointing, isn't it? However,
there are good reasons for this rule.

I've developed two techniques to accomplish what you are trying to do.

The first is to pass a function in a constructor's parameter list. Then the
base class can call a function provided by the derived class. The function
may not be a non-static member of the derived class, but it can be a static
member function that takes a base class reference as a parameter.

My other approach has been to define a "post init" function in a base class
which is called by public constructors of both the base and derived class.
This requires awkward protected constructors in base classes that don't call
the "post init" function and whose only purpose is to initialize the base
sub-object in the derived object's constructor.

You may have a hard time enforcing at compile time that the base class's
public constructor is not called by the derived class's constructor, but you
can place a runtime check to assert the "post init" function is not called
twice.

Good lick!

Anders.


-- 
Anders Pytte                                   Milkweed Software
PO Box 32                                  voice: (802) 586-2545
Craftsbury, VT 05826                  email: anders@milkweed.com

---
[ 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              ]




