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

by zeion
Tags: class, compare, objects
 Admin P: 23,363 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.
 P: 737 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: class test { public: bool operator==(const test &t); // in this method, see if t is equal to *this. }