Re: equality operator based on position
On Mar 2, 3:53 pm, amirkamer...@yahoo.com wrote:
On Mar 2, 3:34 pm, "toton" <abirba...@gmail.com> wrote:
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 ?
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== .
Amir Kamerkar