小编use*_*533的帖子

重载<<的返回值

#include <iostream>

using namespace std;

struct info {
    info(int x, int y) : x(x), y(y) {}
    int x;
    int y;
};

ostream& operator<<(ostream& out, const info &myinfo){
    out << myinfo.x << "  " << myinfo.y;
    return cout;
}

int main() {
    info a(1,2);
    info b(3,4);
    cout << a << " " << b << endl;
}
Run Code Online (Sandbox Code Playgroud)

即使有不正确的过载,上述程序的输出似乎也很好operator <<.

谁能告诉我这个重载问题的影响是什么?我知道重载函数应该返回out而不是cout,但上面的版本是如何表现的?

c++

3
推荐指数
1
解决办法
119
查看次数

标签 统计

c++ ×1