bool overload problem

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

    bool overload problem



    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.n u;

    me = right_operand.m e;

    return *this;

    }







  • Rob Williscroft

    #2
    Re: bool overload problem

    Chiller wrote in news:51a2c065f1 c4340cf8e9bd201 2a5afcd@news.te ranews.com in
    comp.lang.c++:
    [color=blue]
    >
    >
    > 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.n u;
    >
    > me = right_operand.m e;
    >
    > return *this;
    >
    > }
    >[/color]


    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::opera tor == ( Distance const & rhs ) const
    {
    return ( nu == rhs.nu && me == rhs.me );
    }


    Rob.
    --

    Comment

    • Rob Williscroft

      #3
      Re: bool overload problem

      Chiller wrote in news:51a2c065f1 c4340cf8e9bd201 2a5afcd@news.te ranews.com in
      comp.lang.c++:
      [color=blue]
      >
      >
      > 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.n u;
      >
      > me = right_operand.m e;
      >
      > return *this;
      >
      > }
      >[/color]


      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::opera tor == ( Distance const & rhs ) const
      {
      return ( nu == rhs.nu && me == rhs.me );
      }


      Rob.
      --

      Comment

      • John Harrison

        #4
        Re: bool overload problem

        >[color=blue]
        >
        > 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::opera tor == ( Distance const & rhs ) const
        > {
        > return ( nu == rhs.nu && me == rhs.me );
        > }
        >[/color]

        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


        Comment

        • John Harrison

          #5
          Re: bool overload problem

          >[color=blue]
          >
          > 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::opera tor == ( Distance const & rhs ) const
          > {
          > return ( nu == rhs.nu && me == rhs.me );
          > }
          >[/color]

          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


          Comment

          Working...