I have a set of polymorphic classes, such as:
class Apple {};
class Red : public Apple {};
class Green : public Apple {};
And free functions which compare them:
bool operator==(const Apple&, const Apple&);
bool operator< (const Apple&, const Apple&);
I'm designing a copyable wrapper class which will allow me to use classes Red
and Green
as keys in STL maps while retaining their polymorphic behaviour.
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;
};
I want the Copy<Apples>
type to be as interchangeable with Apples
as possible. There are a few more functions I'll have to add to the Copy
class above, but for now I'm working on a free function for operator==
, as follows:
template<typename Cat>
bool operator==(const Copy<Cat>& copy, const Cat& e) {
return *copy == e;
}
Here is part of my testing code:
Red red;
Copy<Apple> redCopy = red;
Copy<Apple> redCopy2 = redCopy;
assert(redCopy == Red());
But the compiler is telling me
../src/main.cpp:91: error: no match for ‘operator==’ in ‘redCopy == Red()’
How do I get it to recognize my operator== above? I suspect the answer might be in adding some implicit conversion code somewhere but I'm not sure what to do.