在这里我附上了一个最小的案例,在子类中,cmp
将替换父类中的那个,但是它不起作用,它总是从父类中调用那个
#include <iostream>
template <typename T>
class A
{
public:
void tell(T a, T b)
{
std::cout << (cmp (a, b) ? a : b) << " is better" << std::endl;
}
protected:
bool cmp (T & a, T & b)
{
return a > b;
}
};
template <typename T>
class B: public A<T>
{
protected:
bool cmp (T & a, T & b)
{
return a < b;
}
};
int main ( int argc , char **argv )
{
B<int> b;
// should be: 3 is better here
b.tell(5, 3);
return 0;
}