Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!geac!alias!fred!rae From: rae@gpu.utcs.toronto.edu (Reid Ellis) Newsgroups: comp.lang.c++ Subject: Re: Multiple inheritance and type casting Message-ID: Date: 5 Oct 90 14:33:14 GMT References: <5087@uqcspe.cs.uq.oz.au> <2709eabf.5abd@petunia.CalPoly.EDU> Sender: Reid Ellis Reply-To: Reid Ellis Organization: Alias Research, Inc. Toronto ON Canada Lines: 29 iain@batserver.cs.uq.oz.au writes: >If D1::f() cast the formal parameter to type D1 (ie. (D1 >&) b), for example to interrogate private data members, no >problems. However, if X::f() casts to an X& my compiler >complains (TC++ V1.0) saying "Cannot cast from B& to X&". How do you know that the object passed in is really an "X &"? If you are writing some sort of "clone()" method for instance, there is no way to tell if the object being passed in is really of the type you expect. This is a design flaw. There are other ways to do it. What is it you're trying to do? Drunk Dude #3 writes: >void X::f(X &x) {} // standard method def >X x; >Y y; // Y is derived from X >X.f(*(X *)&y); // get a pointer and convert the pointer If Y is derived from X, you should be able to simply say: x.f(&y); // no cast required I have found that casts lead to errors, plain and simple. If you are casting base types to derived types, something is wrong with your design. If you are casting from derived types to base types, you're being silly ;-). Reid -- Reid Ellis 264 Broadway Avenue, Toronto ON, M4P 1V9 Canada rae@gpu.utcs.toronto.edu || rae%alias@csri.toronto.edu || +1 416 487 1383