>(god know's why?)
Why? What if you have a class that you would like to print directly with cout?
Code:
#include <iostream>
class Test
{
int i, j;
public:
Test ( int one, int two ) :
i ( one ), j ( two )
{}
};
int main()
{
Test t ( 10, 20 );
std::cout<< t <<std::endl;
return 0;
}
This will obviously not work because cout isn't defined for objects of class Test. You need to define it yourself:
Code:
#include <iostream>
class Test
{
int i, j;
public:
Test ( int one, int two ) :
i ( one ), j ( two )
{}
friend std::ostream& operator<< ( std::ostream& os, const Test& t )
{
os<< t.i <<" -- "<< t.j;
return os;
}
};
int main()
{
Test t ( 10, 20 );
std::cout<< t <<std::endl;
return 0;
}
Now everything works just peachy.
-Prelude