Topic: RTTI operators ?


Author: hendrik@vedge.com (Hendrik Boom)
Date: Thu, 07 Oct 1993 16:38:11 GMT
Raw View
maxtal@physics.su.OZ.AU (John Max Skaller) writes:
:
: In fact, better:
:
:  if( Mac *mm = dynamic_cast<Mac *>(f) )
:      {
:      // some Mac specific thing
:      }
:
: Declarations are now allowed in 'if' and 'while' statements.
: The scope of the variable is the stuff in the curly brackets.
:
: (As opposed to 'for', where the scope of a declaration
: in the initialiser is the rest of the block containing the for)
:
Is this now official?
Or, at least as official as RTTI?

 hendrik.
--
-------------------------------------------------------
Try one or more of the following addresses to reply.
at work: hendrik@vedge.com,  iros1!vedge!hendrik
at home: uunet!ozrout!topoi!hendrik




Author: pete@borland.com (Pete Becker)
Date: Fri, 8 Oct 1993 21:02:35 GMT
Raw View
In article <marc.065h@offline.be>, Marc Duponcheel <marc@offline.be> wrote:
>
>I don't know anyting about the new RTTI stuff
>like `typeid' and `dynamic_cast'
>but it looks to me as if they somehow depend on each other.
>
>Suppose typeid is given then dynamic_cast is implemented as follows:
>
>template <class X, class Y>
>X *dynamic_cast(Y *y)
>{
> if(typeid(X) <= typeid(*y))
>  return (X *) y;
> return 0;
>}
>
>any comments ?
>

 class A {};
 class B {};
 class C : public B, public A {};

 A *aptr = new C;
 B *bptr = dynamic_cast<B *>(aptr);

The last line gives a valid pointer to the B subobject of the C object
created by new. Using the suggested implementation of dynamic_cast produces
a null pointer.
 -- Pete




Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 9 Oct 93 08:41:17 GMT
Raw View


hendrik@vedge.com (Hendrik Boom @ Visual Edge Software Ltd.) asks

 > maxtal@physics.su.OZ.AU (John Max Skaller) writes:
 > :
 > : In fact, better:
 > :
 > :  if( Mac *mm = dynamic_cast<Mac *>(f) )
 > :      {
 > :      // some Mac specific thing
 > :      }
 > :
 > : Declarations are now allowed in 'if' and 'while' statements.
 > : The scope of the variable is the stuff in the curly brackets.
 > :
 > : (As opposed to 'for', where the scope of a declaration
 > : in the initialiser is the rest of the block containing the for)
 > :
 > Is this now official?
 > Or, at least as official as RTTI?

Yes. It was voted in. It is the key RTTI (Run-Time Type Identification)
mechanism. It has been implemented several times now. It will undoubtedly
appear in a compiler near you soon.

In my opinion, it ought to be used with restraint and only where techniques
based on static types can't be used effectively.

See my discusion of RTTI in

 A B. Stroustrup and D. Lenkov
 Run-Time Type Identification for C++
 The C++ Report, Vol 4 No 3, March/April 1992
or
 B. Stroustrup and D. Lenkov
 Run-Time Type Identification for C++ (Revised)
 Proc USENIX \*C Conference, August 1992
or
 B. Stroustrup
 Library Design Using C++
 The C++ Report. Vol 5 no 5, June 1993
or
 The C++ Programming Language (2nd Edition)




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 2 Oct 1993 20:37:55 GMT
Raw View
In article <1993Sep28.212817.16190@borland.com> pete@borland.com (Pete Becker) writes:
>In article <1993Sep28.154952.3561@sifon.cc.mcgill.ca>,
> <whitney@oberon.Meakins.McGill.CA> wrote:
>>
>>Are there any proposals for operators to manage RTTI ?

>> if ( f IS Mac ) {
>>  // some Mac specific thing
>> }
>>
>
> Mac *mm = dynamic_cast<Mac *>(f);
> if( mm )
>     {
>     // some Mac specific thing
>     }

In fact, better:

 if( Mac *mm = dynamic_cast<Mac *>(f) )
     {
     // some Mac specific thing
     }

Declarations are now allowed in 'if' and 'while' statements.
The scope of the variable is the stuff in the curly brackets.

(As opposed to 'for', where the scope of a declaration
in the initialiser is the rest of the block containing the for)

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: marc@offline.be (Marc Duponcheel)
Date: 3 Oct 93 15:43:48 PST
Raw View
In article <1993Oct2.203755.29310@ucc.su.OZ.AU> maxtal@physics.su.OZ.AU (John Max Skaller) writes:
>  if( Mac *mm = dynamic_cast<Mac *>(f) )
>      {
>      // some Mac specific thing
>      }

I don't know anyting about the new RTTI stuff
like `typeid' and `dynamic_cast'
but it looks to me as if they somehow depend on each other.

Suppose typeid is given then dynamic_cast is implemented as follows:

template <class X, class Y>
X *dynamic_cast(Y *y)
{
 if(typeid(X) <= typeid(*y))
  return (X *) y;
 return 0;
}

any comments ?

 -- marc.

################################################################################
 email  preferred address    marc@offline.be [= me@home]
           aka               marc@offline.UUCP ub4b!offline!marc offline!marc
        if [me@home] fails   mdu@abacus.be [= me@brussels.work]
           or                mdp@cimad.be  [= me@antwerp.work]
 fido   2:292/603.26  Marc.Duponcheel@p26.f603.n292.z2.FidoNet.Org [= me@home]
 bix    mduponcheel   mduponcheel@BIX.com [= me@home]
################################################################################





Author: pete@borland.com (Pete Becker)
Date: Tue, 28 Sep 1993 21:28:17 GMT
Raw View
In article <1993Sep28.154952.3561@sifon.cc.mcgill.ca>,
 <whitney@oberon.Meakins.McGill.CA> wrote:
>
>Are there any proposals for operators to manage RTTI ?
>Something along the lines of :
> class Fruit;
> class Apple : Fruit;
> class Mac : Apple;
>
> Mac* m;
> Fruit* f;
> m = new Mac;
> f = m;
> if ( f IS Mac ) {
>  // some Mac specific thing
> }
>

 Mac *mm = dynamic_cast<Mac *>(f);
 if( mm )
     {
     // some Mac specific thing
     }







Author: whitney@oberon.Meakins.McGill.CA ()
Date: Tue, 28 Sep 1993 15:49:52 GMT
Raw View
Are there any proposals for operators to manage RTTI ?
Something along the lines of :
 class Fruit;
 class Apple : Fruit;
 class Mac : Apple;

 Mac* m;
 Fruit* f;
 m = new Mac;
 f = m;
 if ( f IS Mac ) {
  // some Mac specific thing
 }

Whitney