Connecting Tech Pros Worldwide Help | Site Map

RTTI

  #1  
Old July 23rd, 2005, 03:00 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a
Hey all,

Does standard C++ support RTTI? For example, I would like to
dynamically
get a class's name.

Rick

  #2  
Old July 23rd, 2005, 03:00 AM
John Carson
Guest
 
Posts: n/a

re: RTTI


<gouqizi.lvcha@gmail.com> wrote in message
news:1110781594.805104.198220@z14g2000cwz.googlegr oups.com[color=blue]
> Hey all,
>
> Does standard C++ support RTTI? For example, I would like to
> dynamically
> get a class's name.
>
> Rick[/color]

Yes.

--
John Carson
  #3  
Old July 23rd, 2005, 03:00 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a

re: RTTI


"gouqizi.lvcha@gmail.com" wrote:[color=blue]
>
> Hey all,
>
> Does standard C++ support RTTI? For example, I would like to
> dynamically
> get a class's name.
>[/color]

look up functions
typeid()
typename()
and the structure 'type_info'

--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #4  
Old July 23rd, 2005, 03:00 AM
Rolf Magnus
Guest
 
Posts: n/a

re: RTTI


gouqizi.lvcha@gmail.com wrote:
[color=blue]
> Hey all,
>
> Does standard C++ support RTTI? For example, I would like to
> dynamically get a class's name.[/color]

Yes. Try typeid(MyClass).name()
However, the name that you get back from this is implementation defined. It
might not be the same as it is defined in your source code.

  #5  
Old July 23rd, 2005, 03:02 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a

re: RTTI


Hi Rolf,

I tried. gcc 3.2 does not give the same as my source code as you said
it.
What can I do if I want to get the exactly the same name as I defined
in code?

Rick

  #6  
Old July 23rd, 2005, 03:02 AM
red floyd
Guest
 
Posts: n/a

re: RTTI


gouqizi.lvcha@gmail.com wrote:[color=blue]
> Hi Rolf,
>
> I tried. gcc 3.2 does not give the same as my source code as you said
> it.
> What can I do if I want to get the exactly the same name as I defined
> in code?
>
> Rick
>[/color]

create a member function "const char * get_class_name();"
  #7  
Old July 23rd, 2005, 03:02 AM
evaned@gmail.com
Guest
 
Posts: n/a

re: RTTI


Why isn't this standardized? Implementation difficulties? Run-time
inefficiencies?

I'm sorta thinking that having the RTTI work properly for derived
classes would require all classes to have vtables, is this why?

  #8  
Old July 23rd, 2005, 03:03 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a

re: RTTI


Have to do like this way.

Do you know why gcc do in such way?

  #9  
Old July 23rd, 2005, 03:03 AM
Greg Comeau
Guest
 
Posts: n/a

re: RTTI


In article <1110833487.493696.61630@g14g2000cwa.googlegroups. com>,
gouqizi.lvcha@gmail.com <gouqizi.lvcha@gmail.com> wrote:[color=blue]
>I tried. gcc 3.2 does not give the same as my source code as you said
>it.
>What can I do if I want to get the exactly the same name as I defined
>in code?[/color]

If I understand what you're asking, you don't, you get whatever
the _vendor_ chooses for it.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
  #10  
Old July 23rd, 2005, 03:05 AM
Ron Natalie
Guest
 
Posts: n/a

re: RTTI


gouqizi.lvcha@gmail.com wrote:[color=blue]
> Hey all,
>
> Does standard C++ support RTTI?[/color]

Yes.[color=blue]
> I would like to
> dynamically
> get a class's name.
>[/color]
It however does NOT support that.
You'll have to write your own virtual method to return the class name.
  #11  
Old July 23rd, 2005, 03:05 AM
Ron Natalie
Guest
 
Posts: n/a

re: RTTI


evaned@gmail.com wrote:[color=blue]
> Why isn't this standardized? Implementation difficulties? Run-time
> inefficiencies?[/color]
Because it's not generally useful. Identifiers in general are compile
time issues. They don't exist in the executables.
  #12  
Old July 23rd, 2005, 03:05 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a

re: RTTI


I think it is quite useful in Polymorphism. For example, Class A has
two subclasses B and C.You have another function with return type as
class A, but the return value maybe an object of class B or class C. So
you have to determine at the run time the return value is actually of
class B or class C . Besides RTTI, how can you achieve this?

  #13  
Old July 23rd, 2005, 03:05 AM
Seanairt
Guest
 
Posts: n/a

re: RTTI


On Tue, 15 Mar 2005 18:28:10 -0800, gouqizi.lvcha@gmail.com wrote:
[color=blue]
> I think it is quite useful in Polymorphism. For example, Class A has
> two subclasses B and C.You have another function with return type as
> class A, but the return value maybe an object of class B or class C. So
> you have to determine at the run time the return value is actually of
> class B or class C . Besides RTTI, how can you achieve this?[/color]

Other than class loading based on name, I really don't believe it is all
that useful. One thing you could do is use the type_info's == or !=
operators to check against known types in order to choose an algorithm. No
need for the class's name.

Or you can use compile-time polymorphism. Assuming
your going to use the type to dynamically choose an algorithm, you could
use templates & template specialization. You'd might
provide a generic behavior for all type other than B or C (in your
example) and specialize on B and/or C.

Conversely, if you want ONLY B & C to be handled you can provide a declaration
w/o an implementation for the most generic case and provide an implementation
for the specializations of B & C. This way you would get a compile-time error
if you attempted to choose your algorithm for any case other B & C.

I haven't even touched on using reinterpret_cast, dynamic_cast and their
like. I think if you dig a little deeper, you'll find whatever your
designing will be a lot better off (certainly nowhere near as brittle)
than if you base your design on a bunch of hard-coded static strings.
Consider, for example, what happens when you further subclass B or C? A
B_derived class is still a B, but will not, certainly, return "B" when you
call name(). I think you can see where this leads. I'll leave you to it.

  #14  
Old July 23rd, 2005, 03:05 AM
Richard Herring
Guest
 
Posts: n/a

re: RTTI


In message <1110940090.127367.38860@f14g2000cwb.googlegroups. com>,
"gouqizi.lvcha@gmail.com" <gouqizi.lvcha@gmail.com> writes[color=blue]
>I think it is quite useful in Polymorphism. For example, Class A has
>two subclasses B and C.You have another function with return type as
>class A, but the return value maybe an object of class B or class C.[/color]

(For a start, that can't be done. If the function signature is A
f(/*...*/); it can only return an object of class A. If it returns a
*pointer* or *reference* to A, then indeed the referenced object can be
of a derived class.)
[color=blue]
> So
>you have to determine at the run time the return value is actually of
>class B or class C . Besides RTTI, how can you achieve this?[/color]

Why would you want to? The usual reason for having polymorphic classes
is so that the same virtual functions in different subclasses can
exhibit different behaviour, *without* the client code having to know
anything about the polymorphism. Determining the actual type tends to
subvert this abstraction.

However, that's also the clue to how you can do this if you really must:
just give the class a virtual function which returns some kind of type
identifier.

--
Richard Herring
  #15  
Old July 23rd, 2005, 03:06 AM
Rolf Magnus
Guest
 
Posts: n/a

re: RTTI


gouqizi.lvcha@gmail.com wrote:
[color=blue]
> I think it is quite useful in Polymorphism. For example, Class A has
> two subclasses B and C.You have another function with return type as
> class A, but the return value maybe an object of class B or class C. So
> you have to determine at the run time the return value is actually of
> class B or class C .[/color]

However, you don't need the name as string for that to work. You only need a
type identification, and you do get that.
[color=blue]
> Besides RTTI, how can you achieve this?[/color]

Put the behavior that you need to depend on the actual type into a virtual
member function of the class.

  #16  
Old July 23rd, 2005, 03:06 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a

re: RTTI


Suppose you have the following case:

a class A (Array) which has two subclasses B(IntArray) and
C(FloatArray). When a function returns an object of class A, the
client code does't know it contains integers or floats. Now suppose A
has a virtual function SetValueByIndex, the client code has to know
the exactly type of A.

Doing RTT here subverts polymorphism, but it seems that polyphosim
cannot do in this case

  #17  
Old July 23rd, 2005, 03:06 AM
usenet@sta.samsung.com
Guest
 
Posts: n/a

re: RTTI


gouqizi.lvcha@gmail.com wrote:[color=blue]
> Hi Rolf,
>
> I tried. gcc 3.2 does not give the same as my source code as you said
> it.
> What can I do if I want to get the exactly the same name as I defined
> in code?
>
> Rick[/color]


Here's a solution that should work for gcc-3.2. Unfortunately it is
not guaranteed to work generically:

Bhat

///////////////////////////////////////////////////
#include <iostream>
#include <typeinfo>
#include <cstring>


using namespace std;


#define TYPEID(x) cout << #x << ": " << strpbrk(typeid(x).name(), \
"_abcdefghijklmnopqrstuvwxyzAB*CDEFGHIJKLMNOPQRSTU VWXYZ") << endl;

struct sampleStruct
{
char c;
short s;
int i;
};


int
main()
{
TYPEID(sampleStruct);

sampleStruct whatsmytype;
TYPEID(whatsmytype);
}

  #18  
Old July 23rd, 2005, 03:06 AM
gouqizi.lvcha@gmail.com
Guest
 
Posts: n/a

re: RTTI


Hi, Bhat:

Your code is working for non pointer type. For pointer type, for
example,
TYPEID(&whatsmytype), gcc 3.3.2 give something begging with P
"P12sampleStruct"

Rick

  #19  
Old July 23rd, 2005, 03:06 AM
Richard Herring
Guest
 
Posts: n/a

re: RTTI


In message <1110988614.982434.160640@g14g2000cwa.googlegroups .com>,
"gouqizi.lvcha@gmail.com" <gouqizi.lvcha@gmail.com> writes

[Please find out how to make Google quote what you're replying to...]
[color=blue]
>Suppose you have the following case:
>
>a class A (Array) which has two subclasses B(IntArray) and
>C(FloatArray). When a function returns an object of class A,[/color]

Bzzzt. A reference or pointer to an object of class A
[color=blue]
>the
>client code does't know it contains integers or floats. Now suppose A
>has a virtual function SetValueByIndex,[/color]

What are you proposing as the argument type for the virtual function?

SetValueByIndex(size_t index, ??? value);

Remember that it has to have the same signature for both B and C. That
will therefore have to be some kind of variant type that can contain
either int or float. At that point your client code again no longer
needs to worry about the details of B and C.
[color=blue]
>the client code has to know
>the exactly type of A.
>
>Doing RTT here subverts polymorphism, but it seems that polyphosim
>cannot do in this case
>[/color]
It sounds as though you're trying to reinvent some kind of "variant" or
"any" class. It's likely that someone has already produced what you
want.

http://www.boost.org

--
Richard Herring
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
RTTI question Chameleon answers 2 January 3rd, 2007 02:45 PM
Can someone give a simple implementation of RTTI in c++? dotNeter answers 5 August 3rd, 2006 06:05 PM
RTTI overhead Agoston Bejo answers 9 July 23rd, 2005 12:26 AM
[LONG]Simple RTTI Kleidemos answers 6 July 22nd, 2005 06:29 PM
RTTI? Rick answers 9 July 22nd, 2005 05:07 AM