Solved

Advanced C++: rtti and serialization (properties)

Posted on 1998-11-05
2
440 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

696 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