代码是这样的:
#include <iostream>
using namespace std;
class Example
{
private:
int test;
public:
Example()
{
cout<<"Constructing.."<<endl;
} //无参构造函数
Example(int _test)
{
test = _test;
cout<<"Constructing..."<<endl;
} //有参构造函数
Example(Example &exa)
{
cout<<"Copy constructing..."<<endl;
}
Example Fun(Example ex){return ex;} //函数的返回值为对象
int getTest(){return test;}
};
int main()
{
Example a(7);
cout<<"a.Test = "<<a.getTest()<<endl;
Example b;
b = a.Fun(a);
cout<<"b.Test = "<<a.getTest()<<endl;
return 0;
}
运行结果是这样: