引用的问题
输出运算符的重载一定要加引用?
可是可以 不过效率很低下 构造了一个临时对象 最后还要析构= =
#include <iostream>//mooc提交用 using namespace std;//mooc提交用 #include <vector> #include <map> class A{ public: A(){cout<<"A()"<<endl;} A(A&){cout<<"A(&)"<<endl;} ~A(){cout<<"~A"<<endl;} }; ostream& operator<<(ostream& out,const A*){out<<"A*的<<"<<endl; return out;} ostream& operator<<(ostream& out,A){out<<"A的<<"<<endl; return out;} //ostream& operator<<(ostream& out,const A&){out<<"const A&的<<"<<endl; return out;} int main(int argc, const char * argv[] ) {//---------------main-------------- A a; cout<<&a; cout<<a; cout<<"------"<<endl; //---------------main-------------- return 0; }
举报