对 << 重载

来源:互联网 发布:下载开票软件 编辑:程序博客网 时间:2024/04/29 08:46

 

// 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 printa 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:

原创粉丝点击