Newsgroups: comp.lang.c++
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!linus!linus!frieda!howells
From: howells@frieda.mitre.org (Timothy P. Howells)
Subject: Re: Casting downward
Message-ID: <1991May27.213839.11643@linus.mitre.org>
Sender: news@linus.mitre.org (News Service)
Nntp-Posting-Host: frieda.mitre.org
Organization: The MITRE Corporation, Bedford, MA 01730
References: <1991May27.203304.4572@agate.berkeley.edu>
Date: Mon, 27 May 1991 21:38:39 GMT


Hi,

  dougo@soda.berkeley.edu (Doug Orleans) wrote about the fact that C++
doesn't permit you to cast down from a virtual base class to one of
its derived classes.

  I had the same problem with the prohibition against casting down
from a virtual base class.  I also tried beating the compiler by first
casting to (void *), but I found that this caused errors in accessing
member values.  I suspect your bugs are indeed related to the kludge.

  My "solution" was to reference all my objects via unions with tags
indicating the types.  Of course this works, but it seems to defeat
the purpose of object oriented programming to a large extent.

  Another possible "solution" is to use a virtual fuction to retrieve
the data value, but it seems silly to use a function call (which must
be resolved at run time) when all you really want is a data reference.
This approach also forces you to declare a stub virtual function in
the parent.

  I'm new to C++.  Am I missing something here, or is this a flaw in
the language?  Since the limitation only applies to *virtual* base
classes, I assume that there is some implementation problem regarding
multiple inheritance.  On the other hand, if unions can be made to
work, why not casting downwards?
