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

In summary, to return a boolean comparison between two object pointers in C++, you need to define a comparison method within the class using the syntax "operator==" and implementing it to check if the two pointers are equal. This method can also be used to check if the objects pointed to by the pointers are identical. Defining the equality operator as part of the class is necessary for it to work on pointers.
  • #1
zeion
466
1
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.
 
Technology news on Phys.org
  • #3
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.
 
  • #4
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.
}
 
  • #5
Okay I think I got it thanks.
 

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

1. How do I compare two objects of the same class in C++?

To compare two objects of the same class in C++, you can overload the comparison operators such as ==, !=, <, >, <=, and >=. This allows you to define how the objects should be compared based on their properties and values.

2. Can I use the default comparison operators to compare two objects of the same class?

No, the default comparison operators in C++ only work for primitive data types such as int, float, and char. To compare objects of the same class, you need to overload these operators to define the comparison logic.

3. How do I define the comparison logic when overloading these operators?

You can define the comparison logic by accessing the properties of the objects and comparing them using conditional statements. You can also use the built-in functions like strcmp() for string comparison.

4. Can I compare two objects of the same class based on a specific property?

Yes, you can define the comparison logic to compare objects based on a specific property. For example, if you have a Person class with a name and age property, you can overload the == operator to compare the names of two Person objects.

5. Is there a built-in function in C++ to compare objects of the same class?

No, there is no built-in function in C++ to compare objects of the same class. You need to define the comparison logic yourself by overloading the comparison operators.

Similar threads

Replies
35
Views
3K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
40
Views
3K
Replies
36
Views
4K
Replies
5
Views
2K
Replies
31
Views
2K
Replies
17
Views
2K
Replies
23
Views
2K
Back
Top