?
Solved

Advanced C++: rtti and serialization (properties)

Posted on 1998-11-05
2
Medium Priority
?
448 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 600 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

Industry Leaders: 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 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