阅读背景:

对C++中对象作为函数返回值时系统自动调用拷贝构造函数的理解

来源:互联网 
 
 

代码是这样的:

#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;
}#include <iostream>
using namespac



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: