Reputation: 119
I am currently reading up on overloading functions and I came across two examples in my book, but no explanation was given as to why they were differently created.
The first example overloads the "==" operator like this:
bool operator==(const Passenger &x, const Passenger&y){
//.......implementation details hidden
}
While the second example overloads the "<<" operator like this:
ostream& operator << (ostream &out, const Passenger& pass){
//.......implementation details hidden
return out;
}
How come the second example uses the '&' symbol and the first one does not? Why can't we just have ostream operator instead of ostream& operator? Why doesn't bool operator use the '&'?
Upvotes: 0
Views: 71
Reputation: 275350
bool operator==(const Passenger &x, const Passenger&y)
ostream& operator << (ostream &out, const Passenger& pass)
adding spaces:
bool operator== (const Passenger &x, const Passenger&y)
ostream& operator<< (ostream &out, const Passenger& pass)
and categorizing:
RETURN TYPE FUNCTION NAME ( PARAMETERS, MORE PARAMETERS )
bool operator== (const Passenger &x, const Passenger&y)
ostream& operator<< (ostream &out, const Passenger& pass)
The first operator's name is operator==
. It returns a bool
, and takes (const Passenger &x, const Passenger&y)
as parameters.
The second operator's name is operator<<
. It returns a ostream&
and takes (ostream &out, const Passenger& pass)
.
A bool
is the name of the low level boolean (true
/false
) type in C++. So ==
takes two const&
(const references) to Passenger
s named x
and y
and returns true
or false
.
Meanwhile, <<
takes on the left an ostream&
(a non-const reference to an ostream
), and takes pass
on the right (a const&
to a Passenger
), and returns an ostream&
(a non-const reference to an ostream
).
One of the conventions of C++ is that std::cout << a << b << c;
can be done to stream data to an ostream
. The operation is considered non-constant (non-const
), and each <<
takes an ostream&
on the left hand side and returns it.
The compiler ends up doing: operator<<( std::cout, a )
first. It takes the return value, and does operator<<( operator<<( std::cout, a ), b )
. Etc. The return value of each call to <<
is used as the left-hand argument to the next call of <<
.
Upvotes: 0
Reputation: 7427
You need the value returned by the <<
operator to be a reference, so that it actually returns the original ostream
object and not a copy of the object. This is why it works to string several <<
calls together:
std::cout << "Hey"; // Prints "Hey" and returns cout
std::cout << passenger; // Calls custom operator and returns cout
std::cout << "What?"; // Prints "What?" and returns cout
Is equivalent to:
std::cout << "Hey" << passenger << "What?";
It doesn't make much sense to return a boolean by reference, since you don't expect to be able to manipulate the value returned by the ==
operator.
Upvotes: 3