了解复制构造函数

本文探讨了C++中的复制构造函数在函数返回值优化情况下的工作原理,通过示例解释了如何避免临时对象的额外复制与销毁,从而提高效率。详细分析了程序的运行过程和输出结果。
摘要由CSDN通过智能技术生成

写出下列程序的运行结果(备注:函数返回值按照优化后不生成临时对象完成)

#include<iostream>

using namespace std;
class Test
{
private:
    int x;
public:
    Test(int x)
    {
        this->x = x;
        cout << x <<" parameter constructed" << endl;}

    Test()
    {
        x = 0;
        cout << x <<" default constructed" << endl;
    }
    
    Test(Test& t)
    {
        x = 0;
        cout << x <<" copy constructed" << endl;
    }

   Test(const Test& t)
    {
        x = 1;
        cout << x <<" const copy constructed" << endl;
    }
    void init(int x)
    {
        this->x = x;
        cout << x <<" init constructed" << endl;
    }
    ~Test()
    {
        cout << x <<" destroied" << endl;
    }
    int GetX()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值