473,657 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

equality operator based on position

Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Poi ntpoints; (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==(cons t point& p1,const point& p2){
return &p1 == &p2;
}

Mar 2 '07 #1
3 1743
toton wrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Poi ntpoints; (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==(cons t 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(co nst point& p1,const point& p2)
{
return &p1 == &p2;
}

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

john
Mar 2 '07 #2
On Mar 2, 3:34 pm, "toton" <abirba...@gmai l.comwrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Poi ntpoints; (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==(cons t 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
Mar 2 '07 #3
On Mar 2, 3:53 pm, amirkamer...@ya hoo.com wrote:
On Mar 2, 3:34 pm, "toton" <abirba...@gmai l.comwrote:
Hi,
I have a struct Point { int x, int y; }
The points are stored in a std::vector<Poi ntpoints; (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==(cons t 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

Mar 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1666
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 objects of class A, ComboBox2 contains objects of class B). What I'm trying to do is determine if a given object is contained in one of the comboboxes, i.e.: Combobox1.Items.Contains(MyA); Combobox2.Items.Contains(MyB); Now the problem is...
6
1823
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 both a and b referring to the same object, which is not exactly the copy constructor semantics in C++. But I can do the following to remedy:
6
1546
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 resulted in no hits I also searched for: XML equality schema (single words) on the same newsgroups gave very little and not-to-the-point links
37
2787
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 behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
7
1968
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. Search of the on-line documentation of "equal" and "same" yielded nothing useful. Thanks,
6
4488
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: SomeRefObject ^ obj1(..initialized somehow); SomeRefObject ^ obj2(..initialized somehow); if (obj1 == obj2) // This compares the objects themselves for equality
2
1544
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 be able to check whether two colors are equal based only on the properties that determine what color will be displayed. Is there a way to do this without writing my own method? Thanks. -- Nathan Sokalski njsokalski@hotmail.com...
16
1657
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 worked for strings. Thanks in advance, Damien
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.