【发布时间】:2015-02-19 12:40:05
【问题描述】:
我创建了“派生”类,它是“基”类的派生类。它正在使用 CRTP。基类包含一个一元和一个二元运算符。派生类正在实现这些虚拟运算符功能。
template <typename T> class Base
{
public:
virtual bool operator==(T operand) = 0;
virtual bool operator!() = 0;
};
class Derived : public Base<Derived>
{
public:
virtual bool operator==(Derived operand){ return true; }
virtual bool operator!(){ return false; }
};
模板函数 notf 和 equalf 用于测试 Derived 类的成员运算符。函数 notf 通过引用获取一个 Base,并调用它的 !操作员。函数 equalf 做类似的事情。
template <typename T> bool notf(Base<T>& x)
{
return !x;
}
template <typename T> bool equalf(Base<T>& x, Base<T>& y)
{
return x == y;
}
主函数调用那些模板函数。
int main()
{
Derived x, y;
cout << notf(x);
cout << equalf(x, y);
return 0;
}
并且在equalf函数上产生C2678错误。编译器说,error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Base<Derived>' (or there is no acceptable conversion)。但我不知道是什么问题,因为 notf 函数运行良好。代码编译除equalf函数外,运行良好。
当我制作equalf函数来显示参数的类型时,它显示“class Derived”和“class Derived”。如果是真的,那为什么错误信息是left-hand operand of type 'Base<Derived>'?
【问题讨论】:
-
CRTP 通过非虚拟方法提供编译时多态性。虚拟方法提供运行时多态性。在您的案例中,CRTP 模式的目的是什么?或者虚拟方法的目的是什么?
-
@Cheersandhth.-Alf 我正在使用 CRTP 在基类中使用类型 T,因为基类中的 operator== 必须采用 T 类型参数。
-
我认为 perhaps this 是你想要做的,但我仍然不清楚你为什么真的想要这样做。
标签: c++ templates inheritance crtp