Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bool overload problem

Reply
Thread Tools

bool overload problem

 
 
Chiller
Guest
Posts: n/a
 
      04-11-2004


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;

}







 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      04-11-2004
Chiller wrote in news:(E-Mail Removed) s.com 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:perator == ( Distance const & rhs ) const
{
return ( nu == rhs.nu && me == rhs.me );
}


Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-11-2004
>
>
> 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: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how-to overload conversion operator EMyEnum -> bool Florian Kaufmann C++ 1 02-17-2012 07:39 PM
Different BOOL definitions problem Sergei Gnezdov C Programming 3 03-30-2005 07:32 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Problem with bool..... Don C++ 4 11-10-2003 02:19 AM



Advertisments