let's imagine a simple class such as this:
class Foo {
public:
Foo(int b) : bar(b) {}
int bar;
friend bool operator==(const Foo& l, const Foo& r);
};
bool operator==(const Foo& l, const Foo& r) {
return l.bar == r.bar;
}
I'm having a weird behaviour with the operator== where comparing an object with another with the same bar
value doesn't return true.
To understand, consider this code:
Foo& findRef(int barToFind); //Assume those functions works correctly
Foo findCopy(int barToFind);
Foo f1(1);
Foo& fRef = findRef(1);
Foo fCopy = findCopy(1);
f1 == fRef; //Will evaluate to false
f1 == fCopy; //Will evaluate to true
Why is that? Did I miss something? This code was tested on Visual Studio 2013.
Thank you
Edit: Provided is the findRef function. The findCopy is exactly the same, except it doesn't return a reference. MyArray is assured to live for the duration of the program.
Foo& findRef(int bar) {
//MyArray of type std::vector<Foo>
auto it = std::find_if(std::begin(MyArray), std::end(MyArray), [=](const Foo& f){ return f.bar == bar; });
assert(it != std::end(MyArray));
return *it;
}