C++中用函数返回值初始类对象时的一个问题

问题:
有一个类,名字是C,假设有一个函数,它的定义为:
C func()
{
     C tmp;
     return tmp;
}

即返回C类型的函数,那么考虑表达式C newC = func();将调用几次构造函数?分别是什么类型的构造函数?


解答:
<<C++ primer>>里有提到这个过程,首先会创建一个临时的类对象,该对象是由复制构造函数生成,参数是tmp。然后再调用复制构造函数初始化newC。所以一共调用了两次复制构造函数。
但现在的gcc不这么处理,会做一个优化。在C函数里有个tmp变量,离开func时不撤销这个对象,而是让newC和这个对象关联起来。也就是说tmp的地址和newC是一样的。


实验:
#include <iostream>
using namespace std;

class test
{
private:
    int num;
    static int total ;

public:
    test()
    {
        num = 0 ;
        cout << "in the default constructor\n" ;
        total ++;
    }
    test(const test& a )
    {
        num = a. num;
        cout << "in the copy constructor\n" ;
        total ++;
    }

    test(int a)
    {
        num = a;
        cout << "in the one-para constructor\n" ;
        total ++;
    }

    test& operator=(const test& a )
    {
        num = a. num;
        cout << "in the assign op\n" ;
        return * this;
    }


    static void print ()
    {
        cout << total << endl;
    }

    int showa ()
    {
       return num;
    }

};

int test:: total = 0;

test testfunc(test t )
{
    cout << "1\n";
    test tmp(5 );
    cout << "address of tmp:" << &tmp << endl ;
    cout << "2\n";
    return tmp ;
}

int main()
{

    test para;
    test result = testfunc (para);

    cout << "address of result:" << &result << endl ;
    cout << "result.a = " << result.showa () << endl;

    cout << "3\n";
    test::print ();

    return 0;
}

那个static变量用来记录一共调用了几次构造函数。


结果:


解释:
第1行是因为test para;调用了默认构造函数。
第2行是因为把para传给函数形参调用了赋值构造函数。
第3行表明进入了testfunc()
第4行tmp调用参数为int的构造函数
第5行输出tmp的地址
第6行表明要离开函数
第7行输出result的地址,发现和tmp的地址是一样的



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值