From 7484152688924776646
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,2c39b838f9d484c1
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-08-30 12:10:05 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.ems.psu.edu!news.cis.ohio-state.edu!news.maxwell.syr.edu!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "Victor Bazarov" <vAbazarov@dAnai.com>
Newsgroups: comp.std.c++
Subject: Re: Virtual Base-class constructor calls
Date: 30 Aug 2002 19:10:05 GMT
Organization: Posted via Supernews, http://www.supernews.com
Lines: 94
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <umvgbph675a1f1@corp.supernews.com>
References: <5fcc5166.0208301024.506d52e3@posting.google.com>
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Delivered-To: std-c++@ncar.ucar.edu
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
X-Complaints-To: newsabuse@supernews.com
Xref: archiver1.google.com comp.std.c++:13497

"Ken Durden" <ken.durden@augusttech.com> wrote...
> Any class which derives from a virtual base-class (through any number
> of levels of inheritance), must call the constructor of the virtual
> base class. The only invocation which is actually used, however, is
> the one for the class actually being constructed.
>
> Thus, in the following example classes B and C must call the A ctor
> even though their invocation is never used since they are never
> allocated.
>
> My only suggestion is that B and C be permitted to not call the A ctor
> since they are both pure virtual and cannot be allocated.

What if I write another class that derives from B but not
from C, and which implements f()?  I will have no way to
initialise A, so I will have to rely on B's constructor for
the initialisation with a number.

>
> The solution I've used below works alright when the arguments are
> simply, but it becomes a little harder when you start taking in
> references to different types, or objects, etc...

What are you referring to?  In your example D is still
abstract (since it doesn't provide a definition of f())
and initialising bases (B and C) in the D's c-tor is
completely unnecessary.

>
> -ken
>
>
> struct A
> {
>   A(int x) {}
>   virtual void f() = 0;
> };
>
> struct B : public virtual A
> {
> B() :
>   A(2) /* not used */

"Not used" by whom?

>   {}
> };
>
> struct C : public virtual A
> {
> C() :
>   A(2) /* not used */
>   {}
> };
>
> struct D : public B, public C
> {
> D() :
>   A(4),
>   B(),
>   C()
>   {}

You can do just

    D() : A(4) {}

But you have to add

    void f();

to be able to instantiate it.

> };
>
> int main()
> {
> D d;

Error: abstract class

> }

Victor
--
Please remove capital A's from my address when replying by mail


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]



