samedi 23 juillet 2016

Dynamically specify which method to use based on a template type

Suppose I have a method which is simplified to this

template<typename t,typename u>
std::shared_ptr<bar> MyClass::getFunct(std::string SomeStr)
{
    .....
    std::map<std::string,std::shared_ptr<foo> > j;
    ....
    std::shared_ptr<u> collection(new u());
    for (auto val : j){
    j.second->getMethodA() //Will return object of type t <----LINE A
    }
} 

Now I am using it as

getFunct<FirstType>("SomeString")
getFunct<SecondType>("SomeString")
getFunct<ThirdType>("SomeString")

Now j.second in Line A has 3 methods in it

j.second->getMethodA() //returns a type of FirstType
j.second->getMethodB() //returns a type of SecondType
j.second->getMethodC() //returns a type of ThirdType

Currently i am using j.second->getMethodA() with template type FirstType

is there anyway for me to specify to use getMethodB if template type is SecondType and usegetMethodCif template type isThirdType`

Aucun commentaire:

Enregistrer un commentaire