Relational Operator Overloading

I want to overload the relational operators (<, >, <=, >=) as member functions but I can't seem to get it to work. I'm not sure how to access the calling member in the definition of the overload. I have tried the keyword this but I get an error saying that this can only be used in a nonstatic member function. How would I go about coding the definition? Thank you for your help.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class A
{
	int i;
public:
	A(int i): i(i) {}

	bool operator<(const A& a) const
	{
		return i < a.i;
	}

};

int main()
{
	A a1(3);
	A a2(5);

	std::cout << std::boolalpha << (a1 < a2) << '\n';

	return 0;
}
true


EDIT: Ammended to fix constness (see below)
Last edited on
Needs that extra bit of const-ness!

1
2
3
4
5
6
7
8
	...

	bool operator<(const A& a) const
	{
		return i < a.i;
	}

	...
Last edited on
Isn't necessary, but good point - use const wherever you can.
It is necessary, if you want to be able to call the function with a left-hand side that is const.

A program that is only half const correct is 100x harder to write and maintain than one that is fully const correct.
When I try to access the member variables of my class it says that they are undefined. Here is my definition of the overload where "dollars" and "cents" are the member variables. They are declared as private but I don't see why that would be an issue since I am overloading the operator as a member function.
1
2
3
4
5
6
7
8
9
const bool operator >(const Money& amount1) //Says there are too few arguments for the operator
{
	if(dollars > amount1.getDollars()) return true; //This dollars is  said to be undeclared
	else 
	{
		if (dollars == amount1.getDollars() && cents > amount1.getCents() return true; //This cents is the one that says is undeclared as well as the first dollars
		else return false;
	};
}


The comments are the errors that I am getting. It seems to not like that there is only one operator in the definition and it doesn't like it when there are two operators in the declaration.
Last edited on
If this last code snippet is supposed to be a member outside defined outside of the class body, you need the class name. That is:

1
2
3
bool Money::operator >(const Money& amount1) const
{
	...

(I presume here that the method is also declared within the class body)

Andy

P.S. There is no need for a const when returning by value. Just return a bool, not a const bool. But you should have a const at the end of the function header.
Last edited on
Thank you! I can't believe I forgot about the class name. Thank you everyone for the help.
@andywestken Well spotted, absolutely right. Needs to be const.

@ddwinters45
What you have there is not a member function declaration. For that you need to prefix the function name with the class scope:
1
2
3
4
/*const*/ bool Money::operator>(const Money& amount1) const // const goes after the parameters
{
    // ...
}


The reason that the const goes after the parameters is because it is not the return value that is const but the whole function is const. That means that the function is not able to modify the object's member variables.
Last edited on
Topic archived. No new replies allowed.