## C++ how to compare two objects of the same class?

Hi, noob question here,

In C++, how can I return boolean comparison between two object pointers?
ie. return True if the pointers point to same object.

I've tried == but it doesn't work.
Do I need to define a comparison method from within the class?
If so what is the syntax for doing it?

Thanks.
 PhysOrg.com science news on PhysOrg.com >> New language discovery reveals linguistic insights>> US official: Solar plane to help ground energy use (Update)>> Four microphones, computer algorithm enough to produce 3-D model of simple, convex room
 you write a member function called "operator==" http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
 Admin Code: MyClass *p1, *p2; if (p1 == p2) printf("The same object"); if (*p1 == *p2) printf("Identical objects"); The latter check requires you to define and implement == operator for MyClass.

## C++ how to compare two objects of the same class?

The equality operator will work on pointers regardless of whether you've implemented the equality operator for your class. If it's giving an error when used with pointers, then you're doing something wrong.

To define the equality operator as part of your class:
Code:
class test
{
public:
bool operator==(const test &t); // in this method, see if t is equal to *this.
}
 Okay I think I got it thanks.

 Similar discussions for: C++ how to compare two objects of the same class? Thread Forum Replies Programming & Comp Sci 2 Academic Guidance 16 Programming & Comp Sci 2 Differential Geometry 41 Academic Guidance 38