Re: cast operation

Andreas Spengler (spengler@math.uni-frankfurt.de)
Fri, 23 Jul 1999 09:32:50 +0200 (MSZ)


Hi Onuchin,

On Fri, 23 Jul 1999, Onuchin Valeriy wrote:

> Dear C++ experts, I have few questions about cast operation.
>
> problem:
>
> class A
> {
> private:
> Object* theGoal;
> public:
> Object* GetTheGoal() const { return theGoal; }
> ...
> };
>
> class B; // some other class
>
> class C: public A, public B // multiple inherited
> {
> ...
> };
>
> B* d = new C();
>
> // there are 2 choices to get pointer to theGoal
>
> Object* goal1 = ((A*)d)->GetTheGoal(); // cast B to A
> Object* goal2 = ((C*)d)->GetTheGoal(); // cast B to C
>
>
> questions:
>
> 0. what cast from above is correct?

>From my basic C++ knowledge I would say the second version is correct,
because A has no idea of what B is !

> 1. if there are rules to avoid segmentation violation errors?
> 2. what is difference between declarations
> class C: public A, public B
> and
> class C: public B, public A
Could be that the sequence is important when you have the same members or
functions in both classes !

> 3. if cast operation is platform/compiler dependent ?
Since not every compiler is 100% ANSI C++ compatbile this could be !

>
> Excuse me for ignorrance.
> Thank in advance, with best regards, Valery
>
> --
> List archive and information: http://www.troll.no/qt-interest/
>