对 << 重载

    技术2022-05-20  52

     

    // general skeleton of the overloaded output operator

    ostream&

    operator <<(ostream& os, const ClassType &object)

    {

    // any special logic to prepare object

    // actual output of members

    os << // ...

    // return ostream object

    return os;

    }

     

    The ostream is nonconst because writing to the stream changes its state

     

    The Second  parameter is a reference to avoid copying the argument. It can be const because (ordinarily) printing an object should not change it.

     

    Generally, output operators should print the contents of the object, with minimal formatting. They should not print a newline.

     

    IO Operators Must Be Nonmember Functions  .We cannot make the operator a member of our own class. If we did, then the left-hand operand would have to be an object of our class type:


    最新回复(0)