473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

RTTI

Hey all,

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

Rick

Jul 23 '05 #1
18 5856
<go***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com
Hey all,

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

Rick


Yes.

--
John Carson
Jul 23 '05 #2
"go***********@gmail.com" wrote:

Hey all,

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


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
go***********@gmail.com wrote:
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.

Jul 23 '05 #4
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

Jul 23 '05 #5
go***********@gmail.com wrote:
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();"
Jul 23 '05 #6
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?

Jul 23 '05 #7
Have to do like this way.

Do you know why gcc do in such way?

Jul 23 '05 #8
In article <11*********************@g14g2000cwa.googlegroups. com>,
go***********@gmail.com <go***********@gmail.com> wrote:
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.
--
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?
Jul 23 '05 #9
go***********@gmail.com wrote:
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.
Jul 23 '05 #10
ev****@gmail.com wrote:
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.
Jul 23 '05 #11
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?

Jul 23 '05 #12
On Tue, 15 Mar 2005 18:28:10 -0800, go***********@gmail.com wrote:
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.

Jul 23 '05 #13
In message <11*********************@f14g2000cwb.googlegroups. com>,
"go***********@gmail.com" <go***********@gmail.com> writes
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.

--
Richard Herring
Jul 23 '05 #14
go***********@gmail.com wrote:
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.

Jul 23 '05 #15
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

Jul 23 '05 #16
go***********@gmail.com wrote:
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*CDEFGHIJKLMNOPQRSTU VWXYZ") << endl;

struct sampleStruct
{
char c;
short s;
int i;
};
int
main()
{
TYPEID(sampleStruct);

sampleStruct whatsmytype;
TYPEID(whatsmytype);
}

Jul 23 '05 #17
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

Jul 23 '05 #18
In message <11**********************@g14g2000cwa.googlegroups .com>,
"go***********@gmail.com" <go***********@gmail.com> writes

[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

--
Richard Herring
Jul 23 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rick | last post by:
Hi, I wrote a few classes recently and am writing a small GC implementation for C++. I will be implementing my own gc_ptr type maintain a list where I'll store pointers to allocated memory. I...
2
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return...
9
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This...
2
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram...
3
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so...
10
by: Neo | last post by:
what is RTTI? and how to implements it? give me sourceful link for learn and implements it (using standard C mean portable). regards, -aims
3
by: Neo | last post by:
Hi Friends, I have a question about RTTI. 1) In C++ we have vptr pointing to vtables which helps to make use of pointer as polymorphic entities. 2) Without knowing object type I can call methods...
5
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined...
2
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.