正在处理模板函数的简单示例。代码按预期编译和工作。但我的问题是为什么“Cmp”和“Lit”都需要“static”?否则编译不通过?
非常感谢!
template<class T> class Cmp{
public:
static int work(T a, T b) {
std::cout << "Cmp\n";
return 0;
}
};
template<class T> class Lit{
public:
static int work(T a, T b){
std::cout << "Lit\n" ;
return 0;
}
};
template<class T, class C>
int compare(const T &a, const T &b){
return C::work(a, b);
}
void test9(){
compare<double, Cmp<double> >( 10.1, 20.2);
compare<char, Lit<char> >('a','b');
}
最佳答案
C::work(a, b)
命名类 C
的静态成员函数 work()
。
关于c++ - 为什么此模板代码中需要 static 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535462/