I want to use std::find
on a list of a shared_ptr
of an abstract class, but I'm getting an error. Is there even a way to compare two shared_ptr
by dereferencing them in std::find
?
Is it possible to maybe make a friend operator==
that overloads shared_ptr<A>
?
Minimal example:
#include "point.h"
#include <list>
#include <algorithm>
#include <memory>
using namespace std;
class A {
protected:
Point loc;
public:
virtual void foo() = 0;
virtual bool operator==(const Point& rhs) const = 0;
};
class B: public A {
virtual void foo() override{}
virtual bool operator==(const Point& rhs) const override {
return rhs == loc;
}
};
class C {
list<shared_ptr<A>> l;
void bar(Point & p) {
const auto & f = find(l.begin(), l.end(), p); //<-- error is from here
}
};
Error C2679 binary '==': no operator found which takes a right-hand operand of type 'const Point' (or there is no acceptable conversion)
Note: Point
already has operator==
.