0

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.

2

2 Answers 2

1

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:

  1. Unary (-x)
  2. Binary (x - y)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.