c++ operator overload
what i know until now is that we can overload << >> + - ++ ..
but i cannot understand when we declare the operator we put this & before the variable also sometimes we write 2 variables in operator declare
other times when we overload << or >> at operator function we first put ostream and istream (if its >> or <<) first and we return *this which i cannot understand what is it
anyway i need a good tutorial to explain my above queries and general about overloading
Simple google search could give you so many results.
here is one of them:
TUTORIAL: Defining and Overloading Operators in C++
For the "&" question, when dealing with overloading operators we typically have custom objects that we are passing around and these objects are much more efficiently passed around when they are references (which is what the "&" signifies). Passing these objects the other way typically involves wasteful and time-consuming copying of the objects as they are passed into the overloaded operator function code.
Overloading the stream insertion/extraction operators <</>> involves writing to streams. I/O streams in C++ are typically derived from either ostream (ofstream, cout/cerr, etc...) or istream (ifstream, cin, etc...) and so that is what you pass. When you read/write to such streams the stream is passed into the overloaded function along with the object itself to do the actual writing. When the writing is done we need to pass the modified stream back to the caller - this is required because it allows for the chaining of multiple <</>> calls. Without passing the modified stream back we would be unable to chain calls.
You need to read up on references - have a look at your C++ book-of-choice or the one on this site.
Originally Posted by cable