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

  • Context: C/C++ 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    C++ Class Compare
Click For Summary

Discussion Overview

The discussion revolves around how to compare two objects of the same class in C++, specifically focusing on returning a boolean value when comparing object pointers. The scope includes technical explanations and proposed methods for implementing comparison operators.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant asks how to return a boolean comparison between two object pointers in C++, expressing confusion about using the == operator.
  • Another participant suggests writing a member function called "operator==".
  • A third participant provides code snippets demonstrating how to check if two pointers point to the same object and how to check if two objects are identical, noting that the latter requires defining the == operator for the class.
  • One participant asserts that the equality operator works on pointers regardless of whether the equality operator is implemented for the class, suggesting that any errors with pointer comparison may stem from other issues.
  • A participant provides a class definition example for implementing the equality operator.
  • One participant expresses understanding after the explanations.

Areas of Agreement / Disagreement

Participants generally agree on the need to define an equality operator for comparing object contents, but there is some contention regarding the use of the == operator on pointers and the circumstances under which it may fail.

Contextual Notes

Some assumptions about pointer behavior and operator overloading may not be fully explored, and the discussion does not clarify all potential errors that could arise when using the == operator with pointers.

zeion
Messages
455
Reaction score
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
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.
 
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 threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
89
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K