Cant wait to know why 1 thing is working but the other is not
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message = "Hello World!\n";
operator<<(cout, message); // this works =]
int x = 2;
operator<<(cout, x); // so amazingly interesting why this is not =/
}
|
Also while this is a topic want to ask about overloaded operators.
1st question
My_class operator+(const My_class & ...) const {}
In this case i understand that its like method (
operator+
) that will take
My_class
object as argument and will return another
My_class
object. Its like making normal method.
ostream& operator<<(ostream& os, const My_class& ....) {}
Here im loosing myself.
From examples i understand that operator<<(cout, x); is the same as cout << x;
But on what are we actually using this method operator<< (previosuly it was rasy to understand --- >
My_class.operator+(const My_class & ...
))
Now we are calling this method
operator<<
on pure air
2nd question
When exactly this
cout
object is being created and is it really an object?
In My_class example we have to actually create My_class object to give it as argument to
operator+
. Now we just insta give it as argument to
operator<<(cout, x)
.
Explanation of this would mean a lot to me :)