I'm just beginning to learn basic C++ syntax and I'm slightly confused on a piece of code I came across.
For a class created called MyString, there was an operator overloading defined as:
ostream& operator<<(ostream& os, const MyString& s)
{
os << s.data;
return os;
}
Then in some driver function the statement:
cout << s3 << endl;
Is ran, where s3 is of object type MyString. The result prints out the value of s3.
I don't quite understand how this statement functions. After messing with it it seems like a copy constructor is called once and then 3 objects are deconstructed. How exactly does this line work? It seems like the operator accepts a reference to an ostream and MyString, but isn't endl neither? Also why would there only be 1 copy constructor called when there are two instances of the "<<" used? Maybe I'm not even asking the right questions or my questions don't even make sense because I feel really confused about what is going on in these lines. If that is the case, can someone please expound on just a general explaination of what is going on?