我怎样才能实现功能Foo
以分派到正确的功能?
以下代码重现了该问题,并在注释中获得了一些附加信息。
#include <string>
class Base
{
public:
virtual ~Base(){};
};
template<typename T>
class Derived : public Base
{};
void DoSomething(const Derived<std::string> &){};
void DoSomething(const Derived<int> &){};
void Foo(Base *test)
{
// How to call correct DoSomething method?
// (I can't change class Base, but I know that test points to some instance of class Derived)?
// if(test points to an instance of Derived<int>)
// call void DoSomething(const Derived<int> &){};
// else if(test points to an instance of Derived<std::string>)
// call void DoSomething(const Derived<std::string> &){};
}
int main(int argc, char* argv[])
{
Base *test = new Derived<int>;
Foo(test);
delete test;
return 0;
}