ostream是标准的输出流,经常使用cout直接输出,但我认为cout不会直接打印类
下面的代码允许直接将符号重载到“”并直接打印类
Point.h
# ifndef _ point _ h _ # define _ point _ h _ # includeiostreamusingnamespacestd; class point { public : friendostreamoperator (ostream OS,const Point p ); point(intx,int y ); private: int m_iX; int m_iY; (; #endif Point.cpp
# include iostream # include ' point.h ' ostream operator (ostream OS, const Point p ) const修饰的表面是输入型参数(returnOS'x='p.)//返回的是cout point } point :3360 point (intx,int y ) 3360
#include'point.h'intmain(void ) pointp ) 2,1 ); cout p; //安装系统的打印返回0; }