问题描述
|
我有一组多态类,例如:
class Apple {};
class Red : public Apple {};
class Green : public Apple {};
以及比较它们的免费功能:
bool operator==(const Apple&,const Apple&);
bool operator< (const Apple&,const Apple&);
我正在设计一个可复制的包装器类,该类将允许我将类Red
和Green
用作STL映射中的键,同时保留它们的多态行为。
template<typename Cat>
class Copy
{
public:
Copy(const Cat& inCat) : type(inCat.clone()) {}
~Copy() { delete type; }
Cat* operator->() { return type; }
Cat& operator*() { return *type; }
private:
Copy() : type(0) {}
Cat* type;
};
我希望Copy<Apples>
类型与Apples
尽可能互换。我还必须在上面的Copy
类中添加一些其他功能,但是现在我正在为operator==
开发一个免费功能,如下所示:
template<typename Cat>
bool operator==(const Copy<Cat>& copy,const Cat& e) {
return *copy == e;
}
这是我的测试代码的一部分:
Red red;
Copy<Apple> redCopy = red;
Copy<Apple> redCopy2 = redCopy;
assert(redCopy == Red());
但是编译器告诉我
../src/main.cpp:91: error: no match for ‘operator==’ in ‘redCopy == Red()’
如何识别上面的操作员==?我怀疑答案可能是在某处添加一些隐式转换代码,但我不确定该怎么做。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)