equality operator based on position

T

toton

Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Point> points; (global vector)
I want to add equality (operator == ) for the point, which will check
equality based on the position of the point in the vector rather than
its x,y or any other criterion. Thus 2 free point (which are not in
the vector are always unequal ) and so on.
How to add this kind of equality operator ? Is comparing memory
location like this is ok ?
bool operator==(const point& p1,const point& p2){
return &p1 == &p2;
}
 
J

John Harrison

toton said:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Point> points; (global vector)
I want to add equality (operator == ) for the point, which will check
equality based on the position of the point in the vector rather than
its x,y or any other criterion. Thus 2 free point (which are not in
the vector are always unequal ) and so on.
How to add this kind of equality operator ? Is comparing memory
location like this is ok ?
bool operator==(const point& p1,const point& p2){
return &p1 == &p2;
}

It's legal C++, and it will 'work', but I'm not sure it's a good idea,
because it's potentially confusing.

Why do you need to define operator==, why not just define

bool same_address(const point& p1,const point& p2)
{
return &p1 == &p2;
}

and use same_address where you were intending to use operator==

john
 
A

amirkamerkar

Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Point> points; (global vector)
I want to add equality (operator == ) for the point, which will check
equality based on the position of the point in the vector rather than
its x,y or any other criterion. Thus 2 free point (which are not in
the vector are always unequal ) and so on.
How to add this kind of equality operator ? Is comparing memory
location like this is ok ?
bool operator==(const point& p1,const point& p2){
return &p1 == &p2;



}- Hide quoted text -

- Show quoted text -

What do you mean by position in vector ?
Do you want to check the presence of point object in vector ?
You are going to store objects in vector. So if you push an object in
vector a copy is going to get inserted (And hence a different
address). Also, if the vector is required to grow it's going to create
new objects and discard the existing objects.

Amir Kamerkar
 
T

toton

What do you mean by position in vector ?
something like, a point is stored in 5th position in a vector.
Do you want to check the presence of point object in vector ?
No, I want to check 2 points present in a vector are equal in index or
not (essentially they are same object or not).
You are going to store objects in vector. So if you push an object in
vector a copy is going to get inserted (And hence a different
address).
That is there, I am not comparing with copy , so this is more like
identity rather than equality (just like java equality , which makes 2
objects equal if they are identical objects)
Also, if the vector is required to grow it's going to create
new objects and discard the existing objects.
Still, that doesn't matter. It is something like &point1 == &point2 or
not. I named it identical rather than operator== .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top