Topic: RTTI: is before() same as dynamic_cast<>()


Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/10/02
Raw View
>>>>> "AB" == Alex L Bangs <bangs@cs.utk.edu> writes:
[...]
AB>                Base
AB>                /  \
AB>               A    B
AB>                     \
AB>                      C
AB>                       \
AB>                        D

AB> I want to have typeid(A).before(typeid(D)) return false
[...]

type_info::before() defines a total ordering on C++ types used in a
program. An implementation is free to use whatever total ordering it
likes, and does not need to take inheritance structures into account.
(Hence, type_info::before() is unlikely to be of use to you in for
the purpose you outline.)

 Daveed


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Pete Becker <pbecker@oec.com>
Date: 1996/10/03
Raw View
Dag Bruck wrote:
>
> In article <3252D2B2.5A61@oec.com>, Pete Becker  <pbecker@oec.com> wrote:
> >
> >before() provides a consistent ordering that you can use if you store
>                                                                  ^sort
> >typeid objects. It is not required to have any relationship whatsoever
> >to your class hierarchy. That's the reason its name isn't <.
>
> Btw, I don't think there is a way to store typeid object on file, neither
> that there is a public mechanism for creating such objects.

Correct. You would store a pointer or a reference to a typeid object.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Chelly Green <chelly@eden.com>
Date: 1996/10/03
Raw View
Alex L. Bangs wrote:
>
> I've been working through a problem where I would really like to use
> type_info::before() in order to determine an is-a relationship.

type_info::before() is only meant for sorting, where the actual ordering
is unimportant, but the fact that there is a stable ordering is.

...
>                Base
>                /  \
>               A    B
>                     \
>                      C
>                       \
>                        D
>
> I want to have typeid(A).before(typeid(D)) return false -- in my case it
> returns true. I know that dynamic_cast would work here -- why not have
> before have the same behavior?

Because it would impose unnecessary performance penalties, and would not
work for sorting (what does it return for types not related?).
dynamic_cast<> already exists, why not use it? I don't think RTTI was
meant as a way to get a class hierarchy diagram at run-time.

--
Chelly Green | chelly@eden.com | C++ - http://www.eden.com/~chelly
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/10/04
Raw View
Pete Becker <pbecker@oec.com> writes:

> Dag Bruck wrote:
> >
> > In article <3252D2B2.5A61@oec.com>, Pete Becker  <pbecker@oec.com> wrote:
> > >
> > >before() provides a consistent ordering that you can use if you store
> >                                                                  ^sort
> > >typeid objects. It is not required to have any relationship whatsoever
> > >to your class hierarchy. That's the reason its name isn't <.
> >
> > Btw, I don't think there is a way to store typeid object on file, neither
> > that there is a public mechanism for creating such objects.
>
> Correct. You would store a pointer or a reference to a typeid object.

In a file!?  To be read by another process (perhaps on another
processor, over the network).  (I'm also curious as to how you would go
about storing a reference to the typeid object, given that a reference
is not an object, and cannot be accessed in itself.)

--
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Pete Becker <pbecker@oec.com>
Date: 1996/10/07
Raw View
J. Kanze wrote:
>
> Pete Becker <pbecker@oec.com> writes:
>
> > Dag Bruck wrote:
> > >
> > > In article <3252D2B2.5A61@oec.com>, Pete Becker  <pbecker@oec.com> wrote:
> > > >
> > > >before() provides a consistent ordering that you can use if you store
> > >                                                                  ^sort
> > > >typeid objects. It is not required to have any relationship whatsoever
> > > >to your class hierarchy. That's the reason its name isn't <.
> > >
> > > Btw, I don't think there is a way to store typeid object on file, neither
> > > that there is a public mechanism for creating such objects.
> >
> > Correct. You would store a pointer or a reference to a typeid object.
>
> In a file!?  To be read by another process (perhaps on another
> processor, over the network).  (I'm also curious as to how you would go
> about storing a reference to the typeid object, given that a reference
> is not an object, and cannot be accessed in itself.)

Whoops, sorry, I responded to the last clause in the sentence and
completely overlooked the first. You cannot create a typeid object, but
can store a pointer or reference to one. I don't know of any way to
store one in a file.
 -- Pete


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bangs@cs.utk.edu (Alex L. Bangs)
Date: 1996/10/02
Raw View
I've been working through a problem where I would really like to use
type_info::before() in order to determine an is-a relationship. I want to
do this with type_info so I can pass the type as a parameter to a member
function, and since I don't have templatized member functions yet, I need
before to make this work.

Here's the rub: I'm under the impression that before() doesn't work as
flexibly as dynamic_cast. It doesn't in the library I am using, but it is
written to an old C++ spec, so maybe this has been fixed. Specifically,
does before() only work when comparing types that are directly related to
one another -- i.e. one is derived from the other?

I have a situation like this:

               Base
               /  \
              A    B
                    \
                     C
                      \
                       D

I want to have typeid(A).before(typeid(D)) return false -- in my case it
returns true. I know that dynamic_cast would work here -- why not have
before have the same behavior?

Thanks for any help/insight...

-- Alex

--
Alex L. Bangs
bangs@cs.utk.edu
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Pete Becker <pbecker@oec.com>
Date: 1996/10/02
Raw View
Alex L. Bangs wrote:
>
> I've been working through a problem where I would really like to use
> type_info::before() in order to determine an is-a relationship. I want to
> do this with type_info so I can pass the type as a parameter to a member
> function, and since I don't have templatized member functions yet, I need
> before to make this work.
>
> Here's the rub: I'm under the impression that before() doesn't work as
> flexibly as dynamic_cast. It doesn't in the library I am using, but it is
> written to an old C++ spec, so maybe this has been fixed. Specifically,
> does before() only work when comparing types that are directly related to
> one another -- i.e. one is derived from the other?
>
> I have a situation like this:
>
>                Base
>                /  \
>               A    B
>                     \
>                      C
>                       \
>                        D
>
> I want to have typeid(A).before(typeid(D)) return false -- in my case it
> returns true. I know that dynamic_cast would work here -- why not have
> before have the same behavior?

before() provides a consistent ordering that you can use if you store
typeid objects. It is not required to have any relationship whatsoever
to your class hierarchy. That's the reason its name isn't <.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]