bool overload problem

Discussion in 'C++' started by Chiller, Apr 11, 2004.

  1. Chiller

    Chiller Guest

    I wish to implement overloads of six comparison operators (==, !=, <,
    <=, >, >=), which will return true or false when comparing two distance
    values.

    I've added the code below to my Distance class (posted earlier in another
    thread)
    to do the == overload, it compiles correctly but
    I'm not sure how to make it return the bool true or false. Could someone
    please explain what I'm doing wrong and how to correct the problem.

    Thanks,

    Distance & Distance :: operator== (Distance const & right_operand)

    {

    nu = right_operand.nu;

    me = right_operand.me;

    return *this;

    }
     
    Chiller, Apr 11, 2004
    #1
    1. Advertising

  2. Chiller wrote in news: in
    comp.lang.c++:

    >
    >
    > I wish to implement overloads of six comparison operators (==, !=, <,
    > <=, >, >=), which will return true or false when comparing two distance
    > values.
    >
    > I've added the code below to my Distance class (posted earlier in another
    > thread)
    > to do the == overload, it compiles correctly but
    > I'm not sure how to make it return the bool true or false. Could someone
    > please explain what I'm doing wrong and how to correct the problem.
    >
    > Thanks,
    >
    > Distance & Distance :: operator== (Distance const & right_operand)
    >
    > {
    >
    > nu = right_operand.nu;
    >
    > me = right_operand.me;
    >
    > return *this;
    >
    > }
    >



    What you have above appears to be an assignment operator (=), assignment
    an equality (==) are unrelated.

    class Distance
    {
    // other stuff

    public:

    bool operator == ( Distance const &rhs ) const;
    };

    bool Distance::eek:perator == ( Distance const & rhs ) const
    {
    return ( nu == rhs.nu && me == rhs.me );
    }


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Apr 11, 2004
    #2
    1. Advertising

  3. >
    >
    > What you have above appears to be an assignment operator (=), assignment
    > an equality (==) are unrelated.
    >
    > class Distance
    > {
    > // other stuff
    >
    > public:
    >
    > bool operator == ( Distance const &rhs ) const;
    > };
    >
    > bool Distance::eek:perator == ( Distance const & rhs ) const
    > {
    > return ( nu == rhs.nu && me == rhs.me );
    > }
    >


    That looks good but is not correct because me is a units type. For instance
    the OP will want Distance(1000, m) == Distance(1, km).

    Chiller, you need something similar in concept to Rob's example, but you
    must handle the conversion of distances between different units within your
    code, for instance you could convert all distances to metres and then use ==
    to compare the converted distances. A similar approach will work for <, <=
    etc.

    john
     
    John Harrison, Apr 11, 2004
    #3
    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. Don

    Problem with bool.....

    Don, Nov 10, 2003, in forum: C++
    Replies:
    4
    Views:
    395
    John Ericson
    Nov 10, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  3. Sergei Gnezdov

    Different BOOL definitions problem

    Sergei Gnezdov, Mar 30, 2005, in forum: C Programming
    Replies:
    3
    Views:
    315
    Keith Thompson
    Mar 30, 2005
  4. Florian Kaufmann
    Replies:
    1
    Views:
    480
    Victor Bazarov
    Feb 17, 2012
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page