模板编程用的很少,今天碰到一个。
问题是这样的: 我想要在一个模板类中,特化一个成员函数,不知道能不能做到?
template<typename T> class A {
void Foo() {
// some common steps
...
// a special step
Bar();
}
void Bar();
}
对于类型T1和类型T2来说, 他们的Bar() 中的操作是不一样的。
不知道怎样才能做到?
========================
我现在暂时把Bar() 写成了一个非成员的模板函数,但是感觉好像很丑,而且这样不能用到成员变量。
You can specialize only a certain member function of a class template
No way, you have to put the Bar outside, for example:
I haven’t written C++ for a long time, but friend function definitely solves the problem of member variable access.
Here are samples and explanations about template functions
Hope it is useful to you