I was wondering how many parameters can an overloaded operator take in C++?
I've seen operators take both one and two parameters, so I wanted to know if they can take both or just one, specifically for the -
and <<
operators.
I was wondering how many parameters can an overloaded operator take in C++?
I've seen operators take both one and two parameters, so I wanted to know if they can take both or just one, specifically for the -
and <<
operators.
The <<
always takes one parameter. E.g. with x << y
, x
would be the instance operator<<()
is called from and y
would be its parameter. Of course, you could overload the operator with different types of y
, but always only one.
The -
operator has two flavors, and is indeed overloaded with different number of arguments:
-x
)x - y
)For the minus operator, it can only take one parameter like so:
object& operator-(const object &ref); //please note the syntax and use of const
For the << operator (called ostream), you overload it like so, it takes two parameters:
friend ostream& operator<<(ostream &str, const object &ref);
Hope that answers your question.