Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Pointpoints; (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;
} 3 1728
toton wrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Pointpoints; (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
On Mar 2, 3:34 pm, "toton" <abirba...@gmail.comwrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Pointpoints; (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
On Mar 2, 3:53 pm, amirkamer...@yahoo.com wrote:
On Mar 2, 3:34 pm, "toton" <abirba...@gmail.comwrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Pointpoints; (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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matt Burland |
last post by:
I'm a little confused about the way the default equality operator works with
classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains...
|
by: benben |
last post by:
I am a C++ guy recently migrated to C#. One of the thing I don't understand
is assignment (=) and equality (==) operators. If I try to do the following:
C a = new C;
C b = a;
Then I will get...
|
by: onetitfemme |
last post by:
Hi *,
I have been looking for a definition or at least some workable concept
of "XML equality".
Searching on "XML equality" in comp.text.xml, microsoft.public.xsl and
microsoft.public.xml...
|
by: spam.noam |
last post by:
Hello,
Guido has decided, in python-dev, that in Py3K the id-based order
comparisons will be dropped. This means that, for example, "{} < "
will raise a TypeError instead of the current...
|
by: Gary Brown |
last post by:
Hi,
In C#, how do you determine two objects are the "same" rather
than "equal?" In C/C++ you can check the addresses and LISP
provides a rich set of equality operators but C# appears ambiguous....
|
by: Edward Diener |
last post by:
Now that operator overloading allows to ref classes to be compared for
equality using == syntax, how does one compare the actual ref pointers (
^ ) for equality instead ?
As an example:
...
|
by: Nathan Sokalski |
last post by:
Is there a way to check whether two Colors are equal? If the Colors have
different Name properties, the = operator will return false even if the
A,R,G, and B properties are equal. I would like to...
|
by: DamienS |
last post by:
In the interests of me saving hair, can someone please explain to me
what's going on below? Why doesn't == work in comparing two int's when
cast as objects? They're the same type.
Note that it...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |