Solved

Advanced C++: rtti and serialization (properties)

Posted on 1998-11-05
2
410 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
Comment Utility
>> 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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now