【发布时间】:2014-07-17 19:02:44
【问题描述】:
我很感激有关如何重载派生类Derived 的比较运算符operator== 的指针,以便它可以扩展到任意数量的基类Base1 , Base2 , Base3 , ...,(参见下面的代码, ideone 上的完整版)。我怀疑可以利用 bost MPL for_each 或一些类似的构造来调用基类(类型)的 list 上的比较。
// Real problem has many more more Base classes
class Derived : public Base1 , public Base2
{
public:
Derived( unsigned& val1 , unsigned& val2 ) : Base1( val1 ) , Base2( val2 )
{
}
// Can the following sequence of steps be generalized
// for an arbitrary number of base classes?
bool operator==( const Derived& rhs ) const
{
const Base1& rhsBase1 = rhs;
const Base2& rhsBase2 = rhs;
const Base1& thisBase1 = *this;
const Base2& thisBase2 = *this;
return ( thisBase1 == rhsBase1 ) && ( thisBase2 == rhsBase2 );
}
};
编辑
我不能使用 C++11(抱歉遗漏)。
【问题讨论】:
-
不是答案,但
Base1::operator== (rhs)和Base2::operator== (rhs)不是更简单吗?
标签: c++ templates operator-overloading boost-mpl