473,378 Members | 1,411 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,378 software developers and data experts.

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

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
5 3001
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: provaands | last post by:
http://www.simplemachines.org/smf/features.php which are the differences regarding phpbb ? It seems good but little diffused and it is not supported by webhosting;
4
by: Senthoorkumaran Punniamoorthy | last post by:
I found this code in the book text processing with Python. But having little difficulty understanding how exactly it works and what should be passed as argument? Can someone decode this for me...
2
by: Adam Schroeder | last post by:
I'm a computer science student looking for a little advice on network programming using TCP/IP? I'm using Borland's 5.02 compiler, but I could use Dev C++ if you advise that... is there any...
1
by: christian | last post by:
Can someone give an example of how to create a timesheet Database? I don't know how to start.
2
by: davidw | last post by:
Hi, I have some files stored in one of my servers, they all don't have suffix, they can be accessed with path like \\share1\1010\1010178 , 101078 is file name, but in some case ,I need access...
0
by: mike | last post by:
Recently I posted a message about deploying an application over a network. I had the .net framework installed on the pcs, the local intranet set to full trust, and I had copied the bin directory to...
1
by: iano | last post by:
loading a combobox with a valueMember and a DisplayMember? Also, is it possible to include another hidden value that I could access when the DisplayMember is selected? Sorry for the junior...
0
by: coosa | last post by:
Dear all; My code is is a bit long but is modular at least. I'm attempting to implement the depth first search for an application that is supposed to: 1- Fetch based on an ID from the database a...
4
by: kirany82 | last post by:
Hi All, i have declared the IP address as.. unsigned int IP = inet_addr("192.168.0.32"); now i want to convert it back to the dot format. plz let me how.. Thanks, Kiran
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.