Solved

Polymorphic object serialization/creation?

Posted on 2000-03-08
5
252 Views
Last Modified: 2010-04-02
Hello all.  I have a question about implementing object serialization in c++.  

The goal is that objects should be able to write out their type (typeid().name()) in addition to their data members.  When the file is read, an object of the appropriate type is allocated and then constructed from the ifstream.  

I've seen remarks about how MFC does this with CRuntimeClass and CObject, but I'd like to stay away from macros.  Assuming that all classes are derived from a base CSerial class, does anyone have any suggestions about how to do this?  Any information appreciated.  Thanks.
0
Comment
Question by:mweagle
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2599549
To read back, you need a 'database' containing the typenames associated with methods to read, write and create objects. This database has to be filled, so you'll need some code to execute at startup, possibly by initializing some static. The use of macro's just simplifies that, but they are not neccesary.
You can avoid macro's for with the use of a template class with a static member.

template<typename T> class RegisterType
{
     static bool is_registered;
};

template<typename T>
bool RegisterType<T>::is_registered = TypeDB::Register(typeid(T).name());

// Somewhere in your implemamtation units you instantiate the template:

template RegisterType<MyClass>;


Another thing, you don't need inheritance. The methods associated with each type to read/write can be associated as functors or function pointers. This has the advantage that you can serialize builtin types and library types that you don't have control over.

I've done a serialization scheme once (with macro's though). I can paste some relevant pieces if you're interested.
0
 

Author Comment

by:mweagle
ID: 2601977
I was going to use inheritance in order to use overloaded Serialization operators.  Then, when I read in a type from the file I could allocate an object of that type, read in the type-specific data, and then return a pointer to the base class.  Does this sound reasonable?  I'm not worried about types that aren't derived from this base class.

However, this is pretty close to what I had in mind.  Is TypeDB::Register a class with a static Register function, a static database (std::map perhaps) of classnames to create functions?  

Any code snippets would be great, especially since I want to force clients of this scheme to implement the needed functions and I'm not clear where the static instantiation has to occur.  Thanks again for your help.
0
 
LVL 5

Accepted Solution

by:
mbormann earned 150 total points
ID: 2611927
Perhaps you can look at
http://www.codefarms.com/ppf/
http://www.swparts.com/products/radixcpp.htm
?

I am a Java guy ,so perhaps if you read these pages it will help you out ,pls feel free to ask me b4 15 March

http://java.sun.com/products/jdk/1.2/docs/guide/serialization/spec/serialTOC.doc.html

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

simple example
http://www.j-nine.com/pubs/Webtech-sep97/object_serialization.htm

If u have any problems refer to
http://www.cs.wisc.edu/~solomon/cs537/java-tutorial.html

Remember to implement a interface (pure virtual class in C++) means it forces the implementing class to have the functions defined in that interface.

I know its not precise but sorry
:(
0
 

Author Comment

by:mweagle
ID: 2613396
I managed to figure out how to do it with a template class and a base CSerial class that does not require (the dreaded ;) macros.  Thanks for the links though.
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2615232
:)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 79
C++ to C# code conversion issue 4 108
Better understanding on C++ Class serialization and formats 9 65
Problem with SqlConnection 4 170
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

809 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