64,662
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class A{
public:
A(){ cout<<"A Default Constructor"<<"\n"<<endl;}
A( A& ){ cout<<"A Left Refrence Constructor"<<"\n"<<endl;}
A(int n, string str):a(n), desc(str){ cout<<desc<<", "<<"A Constructor"<<"\n"<<endl;}
~A(){ a=0; cout<<"A Destructor"<<"\n"<<endl; }
private:
int a=0;
string desc;
};
typedef class A A;
A FuncA()
{
A FirstA(2, "FuncA()");
return FirstA;
}
int main()
{
A SecondA = FuncA();
}
A( A& ){ cout<<"A Left Refrence Constructor"<<"\n"<<endl;}
FuncA(), A Constructor
A Destructor
l4.cpp: In function 'int main()':
l4.cpp:33:19: error: invalid initialization of non-const reference of type 'A&' from an rvalue of type 'A'
A SecondA = FuncA();
~~~~~^~
l4.cpp:10:3: note: initializing argument 1 of 'A::A(A&)'
A( A& ar){ a=ar.a; desc=ar.desc; cout<<"A Left Refrence Constructor"<<"\n"<<endl;}