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?