In C ++, what happens if both objects overload the << operator? a << b

I thought in

 cout << "Hello world" 

cout object has operator overloading, so we can pass strings to the member function of cout objects.

But in some code example, I saw a class that has an operator overload defined in it.

 class GenericPlayer : public Hand { .. friend ostream& operator <<(ostream& os, const GenericPlayer& aGenericPlayer); .. }; ... cout << aGenericPlayer << endl; ... 

Even if it is not, what if both cout and aGenericPlayer overload operator<< ?

+4
source share
4 answers

Even if it is not, if both overload operators are cout and aGenericPlayer <<

std::cout is a std::ostream , so any std::ostream& operator<<(std::ostream, SomeType) will work with std::cout . But the fact is that the second parameter of the operator is different, so the overloads are different. The first "line" is something like

 std::ostream& operator<<(std::ostream&, const char*); 

and second

 std::ostream& operator <<(std::ostream& os, const GenericPlayer& aGenericPlayer); 

So these are different operator overloads, and there is no ambiguity.

+8
source

Firstly, neither cout nor aGenericPlayer can overload anything. These are objects, and overloading is type-based (even if you donโ€™t usually say that type X overloads << , but rather that there is overload << , which can take type X as the second argument).

Secondly, overload resolution is based on all arguments, not just one. There are about twenty different << overloads for std::istream (which is the base class of type std::cout ), but none (at least in the standard library) take GenericPlayer as the second parameter. Therefore, they cannot be used if the second operand is not a GenericPlayer . Similarly, you can have operator<<( int, GenericPlayer const& ) , in addition to what you have; it will be called if the left side was of type int , and the right side of type GenericPlayer . (I can not think of a single case where this will not be an operator overload, but the language of course allows this.)

+2
source

To cout accepted object GenericPlayer , you need to overload operator<< . operator<< also called the insert operator. Therefore, if you take this in context, you insert the result of your custom function into cout . The overloaded operator returns a reference to the original ostream object, which also means that you can combine the inserts. You must overload the insert statement to recognize the ostream object on the left and a GenericPlayer on the right. See Also Overloading <<Operator for your own classes . As for cout , cout is a class ostream object that represents a standard output stream. It corresponds to the cstdio stdout stream. Since cout is an object of the ostream class, we can write characters into it either in the form of formatted data, using, for example, the insert operator (ostream :: operator <), or as unformatted data using the write function, among others

+1
source

what if both overload operators cout and aGenericPlayer <<

None of them overload it; it is overloaded as a regular function (not a member). Note the use of friend in your example; this allows the access function to the inner classes of the class without participation. Therefore, this situation is avoided.

+1
source

All Articles