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.
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.
精彩评论