Topic: Casting to SuperClass


Author: awick@truffula.fp.trw.com (Andy Wick)
Date: 7 Jul 1993 18:39:00 -0400
Raw View
I am trying to find out if it is legal to typecast a class to its
superclass.  Objectcenter (Centerline) has been sitting on their
butts over a week arguing internally if my code is legal or not.
g++ says yes(ie. it compiles and works), Objectcenter (environment)
says no, CC compiles but bus errors.  Of course CenterLine's says
"Just because it works with g++ doesn't mean it is legal c++
blah blah blah."

Quick Example. (Syntax might be a little off since I don't have it in
front of me. :)

class TopLevel
{
public:
   friend ostream &operator<< (ostream &s, TopLevel t);
private:
   int somekindofdata;
}

ostream &operator<< (ostream &s, TopLevel t)
{
   s << somekindofdata;
   return s;
}

class ParallelTopLevel
{
public:
   friend ostream &operator<< (ostream &s, ParallelTopLevel t);
private:
   int somekindofotherdata;
}

ostream &operator<< (ostream &s, TopLevel t)
{
   s << somekindofotherdata;
   return s;
}

class Problem : class ParallelTopLevel, class TopLevel
{
public:
   friend ostream &operator<< (ostream &s, Problem p);
}

ostream &operator<< (ostream &s, Problem p)
{
// Well I want to output the stuff for ParallelTopLevel
// and TopLevel.  And what do you know I all ready have those routines
// So is this legal?
   s << (ParallelTopLevel)p << (TopLevel)p;
   return s;
}
// Centerline will complain about one of these.  If you don't have multi
// inheritance there are NO problems.  Which makes me believe that there is
// a bug with cfront or centerline or both.  The current solution is
ParallelTopLevel &pt = p;
   s << pt << (TopLevel)p;
// Which works.
}

Sorry about such a long message.  If anyone wants the real code I can mail
it to you.  Any comments or suggestions?  Who is right g++ or CC/Centerline?

--
awick@csugrad.cs.vt.edu                             Andy Wick
awick@truffula.fp.trw.com                         Virginia Tech
It may be that your whole purpose in life, is to serve as a warning to others.




Author: bkline%occs.nlm.nih.gov (Bob Kline)
Date: Thu, 8 Jul 93 19:25:52 GMT
Raw View
Andy Wick (awick@truffula.fp.trw.com) wrote:
: I am trying to find out if it is legal to typecast a class to its
: superclass.  Objectcenter (Centerline) has been sitting on their
: butts over a week arguing internally if my code is legal or not.
: g++ says yes(ie. it compiles and works), Objectcenter (environment)
: says no, CC compiles but bus errors.  Of course CenterLine's says
: "Just because it works with g++ doesn't mean it is legal c++
: blah blah blah."

[example omitted]

I can't see any problems with what you're trying to do.  The ARM
talks specifically about casting pointers instead of the objects
themselves in cases involving multiple inheritance, but I don't
see how it would be any different.  At any rate, the OS/2 compilers
we tried it on (GCC, Borland, IBM, Zortech) all handled (a cleaned-
up version of) your sample as expected.
--
/*----------------------------------------------------------------------*/
/* Bob Kline                                      Phoenix Systems, Inc. */
/* bkline@occs.nlm.nih.gov                        voice: (703) 522-0820 */
/*----------------------------------------------------------------------*/