By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,703 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,703 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Aug 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
dotNeter wrote:
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-lit...t.html#faq-5.9 for
reference).

Pierre
Aug 3 '06 #2

P: n/a
dotNeter wrote:
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

Aug 3 '06 #3

P: n/a
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.
Mi*************@tomtom.com wrote:
dotNeter wrote:
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
Aug 3 '06 #4

P: n/a

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

Aug 3 '06 #5

P: n/a
dotNeter wrote:
Thx guys.

See below.

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.