--------------28472C677566
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Dear ROOTers.
The following just to complete the story.
Best regards. Valery
--------------28472C677566
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Received: from ihep.ihep.su by sirius.ihep.su (5.65v4.0/1.1.10.5/27Jan99-0659PM)
id AA06491; Fri, 23 Jul 1999 13:07:11 +0400
Received: from pilt-s.online.no by ihep.su (5.65v4.0/RELCOM-IHEPv4.7)
id AA05053; Fri, 23 Jul 1999 13:06:15 +0400
Received: from localhost (pompel2-s.online.no [148.122.208.36])
by online.no (8.9.3/8.9.1) with SMTP id LAA02544
for <onuchin@sirius.ihep.su>; Fri, 23 Jul 1999 11:02:53 +0200 (MET DST)
Date: Fri, 23 Jul 1999 11:02:53 +0200 (MET DST)
From: Marius Sundbakken <m-sundb@online.no>
Message-Id: <2456780.932720573491.JavaMail.w-epost@localhost>
To: Onuchin Valeriy <onuchin@sirius.ihep.su>
Subject: Re: cast operation
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-Webofficeref: -1356800773619985429
X-Mailer: Nextel Epostleser
> 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?
Are you using a very old compiler? If not, you should stop using
the old C style casts and use the new ones:
const_cast
static_cast
dynamic_cast
reinterpret_cast
I have no C++ book here right now, but I think what you're trying
to do is a cross-cast. You cannot use old C style casts for this!
The pointer will not be correct, so you'll have to use dynamic_cast
instead:
Object* goal1 = dynamic_cast<A*>(d)->GetTheGoal(); // cast B to A
Object* goal2 = dynamic_casr<C*>(d)->GetTheGoal(); // cast B to C
Now, here's the neat thing with dynamic_cast: If the cast isn't
legal, it'll return null. So, you could do it like this:
A* p1 = dynamic_cast<A*>(d);
C* p2 = dynamic_cast<C*>(d);
Object* goal1;
Object* goal2;
if(p1)
goal1 = p1->GetTheGoal();
if(p2)
goal2 = p2->GetTheGoal();
Back to your question: I do not know which cast to use, but if you
try the above, you'll see which one it is, because p1 or p2 will be
null, and the correct cast is then the cast which doesn't return
null.
> 1. if there are rules to avoid segmentation violation errors?
You probably got a segmentation fault because, as I said, old
C style casts can't do cross-casting, that is, casting back and
forth within a multiple inheritance hierarchy.
> 2. what is difference between declarations
> class C: public A, public B
> and
> class C: public B, public A
I don't think there's any difference, but if you're using Qt and
signals and slots, QObject must be the first class:
class D: public QObject, public whatever
> 3. if cast operation is platform/compiler dependent ?
The new cast operators as mentioned above might not be supported
by all compilers yet.
--------------28472C677566--