My program reads from a configuration file and instantiates several classes with the contents of two specific sections for each time.
To initialize Voice class, I’d call:
initializeDomain ("voice", voice_config, voice_config_fields);
and Voice should be initialized as:
Voice voice ( config, config_fields );
To do so, I programmed the following function:
void initializeDomain (string dom, map<string, string> & config, map<string, string> & config_fields)
{
if ( dom == "voice" ) {
Voice voice ( config, config_fields );
return voice;
} else if ( dom == "account" ) {
Account account (config, config_fields);
return account;
}
}
This is obviously not working, as the return type is variable, depending on the class that is instantiated. So, I tried to build a template that could address this need:
template <typename T>
T initializeDomain (string dom, map<string, string> & config, map<string, string> & config_fields)
{
if ( dom == "voice" ) {
T instantiated ( config, config_fields );
} else if ( dom == "account" ) {
T instantiated ( config, config_fields );
}
return instantiated; }
}
But it does not work either. How can I instantiate different classes in the template?
This looks like classic application for Factory Design Pattern. Allocate instances dynamically and return by [smart] pointer:
Edit 0:
The usage for above setup is for polymorphic types, i.e. when you have a common interface to different implementations, so you would invoke virtual methods via pointer, like:
What you are talking about is a bit strange. C++ is strongly-typed language, i.e. you can’t store instances of unrelated types into same variable. The simple case you are describing looks like this:
So I don’t really see what you are trying to accomplish with that template.