我需要根据模板参数的某些静态成员调用具有相同参数的模板成员函数的不同版本。下面是我需要做的一种简化版本:根据模板参数调用不同版本的模板成员函数
class A {
public:
//...
static const char fooString[];
};
const char A::fooString[] = "This is a Foo.";
class B {
public:
//...
static const char barString[];
};
const char B::barString[] = "This is a Bar.";
class C {
public:
//...
static const char fooString[];
};
const char C::fooString[] = "This is also a Foo.";
//Many other classes which have either a fooString or a barString
void doFoo(const char*s) { /*something*/ }
void doBar(const char*s) { /*something else*/ }
template<class T>
class Something {
public:
//This version should be called if T has a static member called "fooString",
//so it should be called if T is either class A or C
void doSomething() { doFoo(T::fooString); }
//This version should be called if T has a static member called "barString",
//so it should be called if T is class B
void doSomething() { doBar(T::barString); }
};
void someFunc()
{
Something<A> a;
Something<B> b;
Something<C> c;
a.doSomething(); //should call doFoo(A::fooString)
b.doSomething(); //should call doBar(B::barString)
c.doSomething(); //should call doFoo(C::fooString)
}
我该如何实现这一目标?
你怎么能DoSomething的()定义了两次? – Jagannath
这是一种伪代码。它并不实际工作,我想要发生的是,只有一个doSomethings将被使用,这取决于是否存在T :: fooString或T :: barString – Mark