== operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sksjava

    == operator

    I have overloaded the "==" operator for a class. But now I want to only
    compare the addresses of the objects, is there is a c++ mechanism to do this
    without casting?

    Ex:- (not compileable)
    void whatever(Someth ing* one, Something* two) // "==" is overloaded for
    class Something
    {
    // want to compare one's and two's addresses
    if ( one == two ) {} // don't want to invoke the "==" operator

    // only solution that I can think of
    void* onev = reinterpret_cas t<void*>(one);
    void* twov = reinterpret_cas t<void*>(two);
    if ( onev == twov ) {}
    }


  • Victor Bazarov

    #2
    Re: == operator

    "sksjava" <sksjava@hotmai l.com> wrote...[color=blue]
    > I have overloaded the "==" operator for a class. But now I want to only
    > compare the addresses of the objects, is there is a c++ mechanism to do[/color]
    this[color=blue]
    > without casting?
    >
    > Ex:- (not compileable)
    > void whatever(Someth ing* one, Something* two) // "==" is overloaded for
    > class Something
    > {
    > // want to compare one's and two's addresses
    > if ( one == two ) {} // don't want to invoke the "==" operator
    >
    > // only solution that I can think of
    > void* onev = reinterpret_cas t<void*>(one);
    > void* twov = reinterpret_cas t<void*>(two);
    > if ( onev == twov ) {}
    > }[/color]

    (a) You cannot change the default behaviour of comparing pointers (you
    can call them addresses, but they are still pointers, a built-in
    C++ type) because you cannot change the behaviour of the operators
    for built-in types. E.G. you cannot overload operator+(int,i nt).

    (b) Even though you cannot change it, why not simply rely on comparing
    pointers provided to you by C++ already? What do you find not to
    your liking in it?

    (c) Any pointer-to-object can be converted to a pointer-to-void without
    a cast (IOW, implicitly), so you can drop the casts from your code.

    V


    Comment

    • Adam Fineman

      #3
      Re: == operator

      sksjava wrote:[color=blue]
      > I have overloaded the "==" operator for a class. But now I want to only
      > compare the addresses of the objects, is there is a c++ mechanism to do this
      > without casting?
      >
      > Ex:- (not compileable)
      > void whatever(Someth ing* one, Something* two) // "==" is overloaded for
      > class Something
      > {
      > // want to compare one's and two's addresses
      > if ( one == two ) {} // don't want to invoke the "==" operator[/color]
      <snip>

      You don't have to do anything. When you compare two pointers, you are
      already comparing addresses, not what they are pointing to.

      Write yourself a small test program and prove this to yourself.

      - Adam

      --
      Reverse domain name to reply.

      Comment

      • sksjava

        #4
        Re: == operator

        "sksjava" <sksjava@hotmai l.com> wrote in message
        news:bugsc7$2r2 $1@news.lsil.co m...[color=blue]
        > I have overloaded the "==" operator for a class. But now I want to only
        > compare the addresses of the objects, is there is a c++ mechanism to do[/color]
        this[color=blue]
        > without casting?
        >
        > Ex:- (not compileable)
        > void whatever(Someth ing* one, Something* two) // "==" is overloaded for
        > class Something
        > {
        > // want to compare one's and two's addresses
        > if ( one == two ) {} // don't want to invoke the "==" operator
        >
        > // only solution that I can think of
        > void* onev = reinterpret_cas t<void*>(one);
        > void* twov = reinterpret_cas t<void*>(two);
        > if ( onev == twov ) {}
        > }
        >
        >[/color]

        My apologies. I am not all there most of the time and even worse today. :-)


        Comment

        Working...