【发布时间】:2014-04-08 17:07:22
【问题描述】:
我有一个在基类 A 中实现的虚函数。 这个函数里面有一个循环。派生类 B 可用,但不会覆盖虚函数,因为任何派生类的代码都是相同的。
不过,我需要在应根据类类型(基类或派生类)调用的虚函数 (local_policy) 中进行额外的条件检查。 现在,我可以实现另一个虚拟成员函数并在派生类中覆盖它,但是由于调用发生在循环内,虚拟开销应该最小化,所以我想我宁愿使用函数模板并将其专门用于任何派生类。
现在的问题是我从 foo() 中将 *this 指针传递给函数模板,并且从不调用特化。 在这种特定情况下,this 指针是 A 类型而不是 B 类型吗?我猜它不是,我的专用函数模板会被调用。
感谢您的帮助!
template < typename T >
bool local_policy(const T* mtype) { return true; }
class A
{
public:
virtual void foo()
{
for(..) {
if(local_policy(this)) //This will call the non-specialised template! Why?
{ /* do something */ }
}
}
/* [...] */
}
class B : public A
{
/* [...] */
//foo() is not overridden here
}
//Specialize the function template for class B
template <>
bool local_policy(const B* mtype) { return false; }
main()
{
B* test = new B; if(!B) return;
test->foo();
}
提前致谢! 最好的
P.S.:我还尝试了一些 C++11,使用普通函数和带有 std::enable_if 的全局模板,仅在它是派生类时启用。没关系,那个也没有像我预期的那样被调用。 :-/
【问题讨论】:
标签: c++ function templates pointers this