Can someone give a simple implementation of RTTI in c++?

D

dotNeter

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 data types, so
I have to implement the RTTI by myself.

I need a simple and effective example to help me decide how to design.

Can someon help me?

thx in advance.
 
P

Pierre Barbier de Reuille

dotNeter said:
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 data types, so
I have to implement the RTTI by myself.

I need a simple and effective example to help me decide how to design.

Can someon help me?

thx in advance.

I think your question should be posted in comp.compiler where people
competent about how to *implement* a compiler might answer. The topic
here is about the usage and the design of C++ (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9 for
reference).

Pierre
 
M

Michiel.Salters

dotNeter said:
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.

Not exactly. RTTI gives you *information about* a type or object at
runtime.
I mean, in my application, I built several self-defined data types, so
I have to implement the RTTI by myself.

No, you don't. The built-in RTTI works just fine. The one thing to
notice is
that if you want to get the dynamic type of an object through a pointer
to
its base class, that base class should have at least one virtual
function.
If you're managing objects through pointers to base classes, you most
likely will have virtual destructors anyway.

Regards,
Michiel Salters
 
D

dotNeter

Thx guys.

Sorry, perhaps my 1st post was confusing, and totally mislead you.

Actually, I just need an approach to get part RTTI-like behaviors, even
no inherited relationship. That's quite simple, I mean, given an
object, and get its type. Enough!

By now, I just have a simple idea - manually record the object and its
type in pair <pObj, type>. The 1st param is the pointer to concrete
object. Based upon these data, I can easily identify which object is of
which type, plus some simple interfaces.
But it's an exhausive work, and is pretty hard to extend, you know.

So, I need a design idea about this prob.
Btw, the programming language I used is Objective C, not supports RTTI
built-in.

Thx in advance.
 
N

Noah Roberts

dotNeter said:
Btw, the programming language I used is Objective C, not supports RTTI
built-in.

As I recall Objective-C has classes as objects, meaning you can
actually assign a class to a variable or return it from a function.
This allowed things such as [[var class] new]. You can't do this in
C++; it just isn't there (for more reasons than the one we are
concerned with here). What we have is what the last two parts of RTTI
stand for, "Type _Information_". There is just no way to store a type
in C++.

Now, to create the kind of behavior that [[var class] new] provides you
can use the Prototype pattern:

class X
{
public:
X* makeNew() { return new X(); }
};

X x;

X* px = x.makeNew();

You will never be able to create an equivelant to [var class] but you
can create equivelents for its common uses through Prototype.

You could create a template as a generic prototype:

template <typename T>
class Prototype
{
public:
T* makeNew() { return new T(); }
T make() { return T(); }
};

Then you could make a map and store these prototypes in it based on
information passed back from RTTI through the use of boost::any or an
abstract base for Prototype. However, better to just learn how your
general problem can be solved in C++ rather than trying to implement it
as you would in Objective-C.

In short, you cannot store a "type" anywhere. You can do certain
things at compile time that resolve types and this provides a
compile-time polymorphism...but that type must be available at time of
compile and not be ambiguous. RTTI will tell you what type you have
but this is not in a form that is useful for generating new objects of
that type...it can be passed to a system that is, but it itself won't.
As a "static language", C++ has rather limited type information at
runtime.
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top