In the following code:
Code:
#include <iostream>
class Base{};
class Bar{
public:
Bar() : fptr(0) {}
const Base* get_origin() const { return fptr; }
private:
Base* fptr;
friend class Foo;
};
class Foo : public Base{
public:
Bar make_bar(){
Bar b;
b.fptr = this;
return b;
}
};
void test_equality( const Base* f1, const Base* f2 ){
if( f1 == f2 )
std::cout << "Pointers are equal.\n";
else
std::cout << "Pointers are not equal.\n";
}
int main(){
Foo f;
Bar b = f.make_bar();
Bar b2 = f.make_bar();
Bar c;
test_equality( b.get_origin(), b2.get_origin() );
test_equality( b.get_origin(), c.get_origin() );
return 0;
}
I want to make sure that a Bar instantiated on its own is never "equal" to a Bar created by a Foo by checking the Base pointer. I had to add the Base class and derive Foo from it because declaring a Foo pointer in Bar results in error( declaration of Foo not found ).
Is there an easier/more-elegant way to achieve this kind of checking paradigm?