In operator overloading?

while cascading of operator, the return type is ostream&.
what is need of address?

and is it compulsory to write “operator” while overloading?

@mehulbhandari358
Operator is a C++ keyword that must be used while overloading an operator.
Its not the address that we are passing for ostream object but rather its reference.

will it work if & this is not being written ?

@mehulbhandari358
It won’t. You cannot create objects of ostream class. You only pass the already created objects to the functions by reference.

i didn’t get that ? can you elaborate why can’t it be done ?

@mehulbhandari358
If you overloaded an operator with one argument simply as ‘ostream’, what the compiler will do is that it will try to create a copy of that object when your function is called.
ostream and istream classes do not permit us to create new objects of them or create copies of them from existing objects. Hence you would get an error for this. The only option is to use the already instantiated objects like cout ( for ostream ) and cin ( for istream ) and pass them by reference to functions so no copies are created.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.