473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.nu;

me = right_operand.me;

return *this;

}



Jul 22 '05 #1
4 1574
Chiller wrote in news:51******************************@news.teranew 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::operator == ( Distance const & rhs ) const
{
return ( nu == rhs.nu && me == rhs.me );
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Chiller wrote in news:51******************************@news.teranew 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::operator == ( Distance const & rhs ) const
{
return ( nu == rhs.nu && me == rhs.me );
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #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::operator == ( 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
Jul 22 '05 #4
>

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::operator == ( 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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
4
by: Chiller | last post by:
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...
18
by: Marcin Kalicinski | last post by:
Hi, Why is there void* conversion to check for std::istream failure bit? Why not a conversion to bool? When I try to return an istream from a function that actually returns bool, I get the...
12
by: Fred | last post by:
Why was the bool type introduced into c++? What does it provide that int does not and are the two entirely interchangable in conditional expressions? Thanks Fred
2
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private...
10
by: Joseph Turian | last post by:
Is it possible to allow a class to be converted to bool? For example, if I have: class foo { private: unsigned i; }; and I want to evaluate: if (foo)
3
by: bonham28c | last post by:
hi, i m trying to make a custom bool class. anything is the same as bool except when it is printing, i want it to print + if true and - if false. but it seems like i cant subclass bool like...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.