RTTI

G

gouqizi.lvcha

Hey all,

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

Rick
 
R

Rolf Magnus

Hey all,

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

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.
 
G

gouqizi.lvcha

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
 
R

red floyd

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

create a member function "const char * get_class_name();"
 
E

evaned

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?
 
G

Greg Comeau

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?

If I understand what you're asking, you don't, you get whatever
the _vendor_ chooses for it.
 
R

Ron Natalie

Hey all,

Does standard C++ support RTTI?
Yes.
I would like to
dynamically
get a class's name.
It however does NOT support that.
You'll have to write your own virtual method to return the class name.
 
R

Ron Natalie

Why isn't this standardized? Implementation difficulties? Run-time
inefficiencies?
Because it's not generally useful. Identifiers in general are compile
time issues. They don't exist in the executables.
 
G

gouqizi.lvcha

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?
 
S

Seanairt

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?

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.
 
R

Richard Herring

In message said:
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.

(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.)
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?

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.
 
R

Rolf Magnus

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 .

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

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

gouqizi.lvcha

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
 
U

usenet

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


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­CDEFGHIJKLMNOPQRSTUVWXYZ") << endl;

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


int
main()
{
TYPEID(sampleStruct);

sampleStruct whatsmytype;
TYPEID(whatsmytype);
}
 
G

gouqizi.lvcha

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
 
R

Richard Herring

In message <[email protected]>,

[Please find out how to make Google quote what you're replying to...]
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,

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

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.
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top