文章目录
1 C++中左移操作符的重载

我们可以重载左移操作符,将变量或者常量左移到一个对象中!
示例代码:
#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;
double a = 0.1;
double b = 0.2;
cout << a + b << endl;
return 0;
}
参考资料:
416

被折叠的 条评论
为什么被折叠?



