C++ 匿名对象的生命周期——强化训练(二)

本文深入探讨C++中的匿名对象及其生命周期,了解临时对象的创建、存储和销毁过程,结合实例解析其在编程中的应用和注意事项。
#include<iostream>
using namespace std;

class ABCD
{
public:
	ABCD()
	{

	}
	ABCD(int a, int b, int c)
	{
		m_a = a;
		m_b = b;
		m_c = c;
		cout << "ABCD类有参构造";
		cout << "a=" << m_a << "b=" << m_b << "c=" << m_c << endl;
	}

	~ABCD()
	{
		cout << "我是ABCD类的析构" << endl;
	}
	int getA()
	{
		return m_a;
	}
private:
	int m_a;
	int m_b;
	int m_c;
};
class Mye
{
public:
	Mye() :abcd1(1, 2, 3), abcd2(4, 5, 6), m(7)
	{
		cout << "我是MYe的构造函数" << endl;
	}
	Mye(const Mye& obj) :abcd1(8, 9, 10), abcd2(11, 12, 13), m(8)
	{
		cout << "我是mye的拷贝构造函数" << endl;
	}
	~Mye()
	{
		cout << "我是Mye的析构函数" << endl;
	}

public:
	ABCD abcd1;
	ABCD abcd2;
	const int m;

};
int dothing(Mye mye)
{
	cout << "dothing() mye.abcd1,getA()" << mye.abcd1.getA() << endl;
	return 0;
}
int run2()
{
	Mye mye;//先调用ABCD类的有参构造函数,再调用mye类的有参构造函数
	dothing(mye);/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值