bool overload problem

C

Chiller

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;

}
 
R

Rob Williscroft

Chiller wrote in 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.
 
J

John Harrison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top