【发布时间】:2016-02-02 22:49:37
【问题描述】:
考虑下面的类,它包装了一个 container 和 type-erases 它的类型:
class C final {
struct B {
virtual bool empty() const noexcept = 0;
};
template<class T, class A>
struct D: public B {
// several constructors aimed to
// correctly initialize the underlying container
bool empty() const noexcept override { return v.empty(); }
private:
std::vector<T, A> v;
};
// ...
public:
//...
bool operator==(const C &other) const noexcept {
// ??
// would like to compare the underlying
// container of other.b with the one
// of this->b
}
private:
// initialized somehow
B *b;
};
我想将 operator== 添加到课程 C。
在内部,它应该简单地在底层容器上调用相同的运算符,但我被这个问题困住了,因为我不知道该怎么做。
这个想法是,如果operator== 的底层容器的operator== 返回true,则C 的两个实例是相等的。
到目前为止,无论我尝试了什么,我最终都无法获得两个底层容器之一的类型,主要是 other 的一个。
是否有一个我目前看不到或者我应该放弃的简单解决方案?
【问题讨论】:
-
请正确建模您的数据(您的嵌套类看起来像嵌套命名空间)
-
我想帮助你,但我会撒谎说我已经理解我的 sn-p 出了什么问题。对不起。它的格式很好,那么到底是什么问题?我忘了
;,确实是固定的。 -
我们缺少
operator==和“包装容器的类...”之间的联系。class C没有容器。 -
嗯,你是对的,等等,我要添加更多细节......
-
无论如何,类型擦除迭代器和
std::equal。
标签: c++ c++11 operator-overloading type-erasure