equality operator based on position

Discussion in 'C++' started by toton, Mar 2, 2007.

  1. toton

    toton Guest

    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;
    }
     
    toton, Mar 2, 2007
    #1
    1. Advertising

  2. toton 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;
    > }
    >


    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
     
    John Harrison, Mar 2, 2007
    #2
    1. Advertising

  3. toton

    Guest

    On Mar 2, 3:34 pm, "toton" <> 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 ?
    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, 2007
    #3
  4. toton

    toton Guest

    On Mar 2, 3:53 pm, wrote:
    > On Mar 2, 3:34 pm, "toton" <> 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
     
    toton, Mar 2, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. italy

    Equality operator

    italy, Mar 5, 2005, in forum: Python
    Replies:
    5
    Views:
    7,002
    Marc 'BlackJack' Rintsch
    Mar 5, 2005
  2. Replies:
    37
    Views:
    771
    Antoon Pardon
    Jan 16, 2006
  3. sathya_me
    Replies:
    14
    Views:
    438
    -berlin.de
    Aug 11, 2004
  4. KOFKS
    Replies:
    2
    Views:
    279
    KOFKS
    Jun 6, 2006
  5. Replies:
    3
    Views:
    184
Loading...

Share This Page