为什么c++中重载流操作符要用友元函数
作为成员函数的话,左操作数非得是自己不可
cout << XXX 这种情况是cout在调用operator<<而不是XXX在调用operator<< 。
如果你要把operator<<设为成员函数就只能用 XXX << cout 这种形式。
所以这样也是可以的,就是有违常理
struct point
{
    int x;
    int y;
    point (int _x, int _y)
        : x(_x), y(_y)
    {}
    std::ostream& operator<<(std::ostream& os) const
    {
        os << "(" << x << ", " << y << ")";
        return os;
    }
};
int main(int argc, const char* argv[])
{
    point p1(1, 2);
    p1 << std::cout; // 写法同p1.operator<<(std::cout);
    return 0;
}
 
                        
                        