[comp.lang.c++] Pointers and virtual base classes

basti@orthogo.UUCP (Sebastian Wangnick) (11/30/90)

I'm stuck with the following problem:

I just switched over to multiple inheritance with Cfront 2.0
with my application. I always use pointers to class objects.
I have a base class ``Object'' from which everything is derived.
I derive class ``NamedObject'' and create an instance of this class
pointed to by ``Object* NIL'', which is used to indicate a special value.

Now everywhere within my other classes, where I use members like
``Array* childs'', when these are not yet set up, I write
``childs = (Array*) NIL''. This condition is then checked by
``if (childs!=NIL)'' (no cast needed).

My problem comes up with multiple inheritance. I have a class
``Gradget'' inheriting from ``Object'' that serves as an abstract
class for graphics primitives. I have two classes ``CompositeGradget''
and ``GCGradget'' inheriting from ``Gradget'' implementing container
functionality resp. the ability to hold and manage a graphics context.
Then I inherit class ``GCComposite'' from both of them. Therefore
class ``Gradget'' must be virtual base class to both ``CompositeGradget''
and ``GCGradget'', mustn't it?

Suddenly I always get error messages like ``cast Object* -> derived GCGradget*;
Object is virtual base'' when I try to assign ``(GCGradget*) NIL'' to any of
my pointers to GCGradgets.

Worse yet, *every* cast from type ``Object*'' to any of my virtually derived
classes is forbidden. It seems that the virtually derived class objects
do not fully behave as ``Object''s anymore. For example, I have a class
``Dependancy'' that uses callbacks to notify class objects of changes 
to other objects. There, too, I need to cast ``Object*'' to ``SomeClass*'';
which failes to work for virtually derived classes.

I was curious and looked what would happen when casting a virtually from 
``Object'' derived class object pointer to any other pointer to a class
derived from ``Object'', virtually or not.
Aha, no compile time error anymore. But lots of *runtime* errors :(
Not even accessing ``Object''s members does work anymore!
Why doesn't the compiler catch these, too?

Help would be appreciated much.

Sebastian Wangnick (basti@orthogo.uucp)

was gone,another, and from