假设我有一个符合 简化到这个动态指定要使用基于模板类型的方法
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){
val.second->getMethodA() //Will return object of type t <----LINE A
}
}
现在的方法,我用它作为
getFunct<FirstType>("SomeString")
getFunct<SecondType>("SomeString")
getFunct<ThirdType>("SomeString")
现在val.second
有3种方法
val.second->getMethodA() //returns a type of FirstType
val.second->getMethodB() //returns a type of SecondType
val.second->getMethodC() //returns a type of ThirdType
当前我使用的是 val.second->getMethodA()
与模板类型FirstType
有反正对我来说,指定要使用getMethodB
如果模板类型是SecondType
和使用getMethodC
如果模板类型为ThirdType
这听起来像XY的问题。你试图解决什么设计问题?绑定功能对象是可能的通过'std :: bind()' – lorro
让我修复那个抱歉 –