jeudi 13 juillet 2017

Usage of template type to call constructor?

I have a (predesigned and unchangeable) abstract factory that I need to work with. Usage is to create a specific type of message that is later sent to a server. It requires a message id to work.

Example:

int messageID = 1000;
StartServerMessage *startServer = (StartServerMessage*)ServerAbstractFactory::CreateObject(messageID);

There are really lots of different messages and I wonder, if it possible to make a short and elegant approach on creating an instance of each message type. Currently I call manually for each type something like this:

//I know at this point i want an IntegrityCheckMessage
int messageID = 2050;
IntegrityCheckMessage* message = (IntegrityCheckMessage*)ServerAbstractFactory::CreaterObject(messageID);

I tried to make something like that:

auto* message = (auto*)ServerAbstractFactory::CreateObject(messageID);

But it seems "auto is not allowed" here. Each message derives at least from abstract class ServerMessage. Some special messages derive from more than this class.

I somehow know I need templates here, but cant think of any way to use them without calling the constructor of the template class (which seems to be not allowed in c++14)

Does anybode know a way to deal with this without implementing a single method for each message type?

Using Windows 10 64bit with Visual Studio 2015 and C++14

Cheers

Aucun commentaire:

Enregistrer un commentaire