Solved

Advanced C++: rtti and serialization (properties)

Posted on 1998-11-05
2
445 Views
Last Modified: 2010-04-02
Hi everybody,

I am trying to build a data structure ressembling a registry for my C++ application ( a client and server implementing the RTSP protocol).
Is it possible to include the name of the class as a strign and have an object of that type made (I think java can do it)?
Also , is it possible to serialize objects in C++ (and for lack of a better option use properties liek in Java)?

Thanks for your help.
Dotand
0
Comment
Question by:dotand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1177079
>> Is it possible to include the name of the class as a strign
You can use RTTI to get the class name, but for RTTI to work properly, the class must have at least on virtual function

>> have an object of that type made.  
yes and no.  What you can do is make any class that you want to serialize "register" a "create function"  This is a function that creates a new default constructed object of that type using new and then returns a pointer to that object.  Each class must register this function along with the class name.  (This information can be stored in an STL map object).  Then when you get the name of what you want to create, you look up the create function and then create a new object.  

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177080
That allows you to (1) determine the type of the object (if it has a virtual function and (2) recreate a default constructed object fo that type (if that type was registered).  These two features are necessary for serialization, but are not enough to impliment it.  You still need a way to write out the object informaiton and a way to read it back into an object after it was created with the create procedure.  In general this is hard to do, but if you are willing to impose one more limitation, it becomes considerably easier.

Can you require that all the objects be in a single class hierarchy?  There are many advantages to this and this is the way that serialization in C++ is ussually handled.

If you have all the objects in a single class hierarchy, then you can add two virtual procedures to the class heirarchy (starting with the base class).  You add a procedure to write the class data out to a stream and another to read it back in from a stream.  Ever class in the hierarchy that has non-static data members must implement these two procedures.  (And in each of these procedures they must call their immediate base class's procedure to have it read/write its data and so on up the class hierarchy).  

Now the create functions will still create derived objects, but they will return pointers whose type is a pointer to the base class. (important later)

To write out an object, you use RTTI  to get the object;'s class name and write that to a stream, then you call the object's virtual write procedure and it will write all the object's data to the stream.

To create an object from a stream, you read the object's class name.  Then you look up its create pointer and call the create procedure.  This returns a pointer whose types is the base class type, however, it really points to a derived class.  Next you call the virtual read procedure and it reads in the object's data.

Let me know if you have questions.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question