我有一些像这样的代码:
class Point {
public:
int x,y;
Point() : x(1), y(1) {}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用printf()以下方式打印该类的对象:
int main()
{
Point point;
printf("%o",point);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者我必须超载operator<<和使用std::cout:
std::ostream& operator<<(std::ostream& os, Point const& p)
{
os << p.x << "," << p.y;
return os;
}
int main()
{
Point point;
std::cout << point;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用打印该类的对象
printf()吗?
号printf是不是在这个意义上扩展.
你最好的选择是operator<<在std::ostream和之间过载Point.
PS我建议将参数类型更改为Point const&.
| 归档时间: |
|
| 查看次数: |
11962 次 |
| 最近记录: |