samedi 7 février 2015

Call a unknown constructor (Replacement for reflection) c++

I'm porting a networking api I made in java/as3 to c++. I'm quite new to c++ so I'm not that familiar with all the tools that are available.


I have a class called Packet. In my java/as3 class that packet has 2 hashmaps, one for classToId and one for idToClass.


Then I register a derived packet with an id and its class object.


When a packet is received I get its packet ID and then create a new packet with that ID.


In as3 code I then use:



public static function getNewPacket(id:int):Packet
{
try
{
var clazz:Class = getClassById[id];
return (clazz == null ? null : Packet(new clazz()));
}
catch (e:Error)
{
trace("Skipping packet with id " + id + ": " + e.message);
}
return null;
}


As you can see, reflection is used to call the constructor. In c++ however, I have no idea how I could create this. I can't use a static list of packets because this is a API class.(So this class will be extended in an application using this API)






Aucun commentaire:

Enregistrer un commentaire