views:

64

answers:

1

I am developing game editor in c++.I have implemented reflection mechanism using DiaSDK.Now I want to store state of the objects(Like Camera,Lights,Static mesh) in some level file via serialization. And later on able to retrieve their state via deserialization.Serializing objects is not a problem for me.During deserialization I am getting class type as a string.So how to create instance of object using that class type string?so that I can create object of that particular type.

+3  A: 

When you serialize the class, you will need to emit its runtime type so that you can instantiate the correct type when deserializing. Otherwise, it is not possible to determine which runtime type to use.

A good technique for constructing classes based on a type string is to build a hash map from class names to factory objects capable of instantiating such a type. You will need to create that mapping based on the types that you might possibly deserialize. If that set of types can change, then a common approach is to provide a means by which a factory may be "registered" or "deregistered" with the map for a particular name.

Michael Aaron Safyan
I think he is taking about creating instance from object type string, say you know object type of "camera" and he wants to create instance of that class.
Xinus
@Xinus, you're right. I believe the question was updated since I originally posted my answer, but I have since updated.
Michael Aaron Safyan