C++深度解析 初探C++标准库 --- cin,cout,std(31)
重载左移操作符
示例程序:
#include <stdio.h>
const char endl = '\n';
class Console
{
public:
Console& operator << (int i)
{
printf("%d", i);
return *this;
}
Console& operator << (char c)
{
printf("%c", c);
return *this;
}
Console& operator << (const char* s)
{
printf("%s", s);
return *this;
}
Console& operator << (double d)
{
printf("%f", d);
return *this;
}
};
//全局对象
Console cout;
int main()
{
cout << 1 << endl;
cout << "D.T.Software" << endl;