4.2.4构造函数调用规则

4.2.4构造函数调用规则


#include <iostream>
using namespace std;
/*
1.创建一个类,c++编译器会给每个类添加3个函数
默认构造
析构函数
拷贝构造
*/
class person4
{
public:
	//person4()
	//{
	//	cout << "默认构造" << endl;
	//}
	~person4()
	{
		cout << "析构构造" << endl;
	}
	person4(int age)
	{
		m_age = age;
		cout << "有参构造" << endl;
	}
	person4(const  person4 &p)
	{
		m_age = p.m_age;
		cout << "拷贝构造" << endl;
	}

	int m_age;
};
void test04()
{
	//person4 p;
	//p.m_age = 18;
	//person4 p2(p);
	//cout << p2.m_age << endl;
}
void test05()
{
	person4 p(0);


}
int main4_2_4()
{
	test04();


	system("pause");
	return 0;
}

4.2.3拷贝构造函数调用时机


#include <iostream>
using namespace std;
/*
三种情况:
	使用一个已经创建完毕的对象来初始化一个新对象
	值传递的方式给函数参数传值
	以值方式返回局部对象
*/
class person3
{
public:
	person3()
	{
		cout << "默认构造" << endl;
	}
	person3(int age)
	{
		m_age = age;
		cout << "有参构造" << endl;
	}
	person3(const person3 &p)
	{
		m_age = p.m_age;
		cout << "拷贝" << endl;
	}

	~person3()
	{
		cout << "析构" << endl;
	}
private:
	int m_age;
};

void test01()
{
	//使用一个已经创建完毕的对象来初始化一个新对象
	//person3 p1(20);
	//person3 p2(p1);

	
	
}
void dowork(person3 p)
{

}
void test02()
{
	//	值传递的方式给函数参数传值
	person3 p;
	dowork(p);
}
person3 dowork2()
{
	person3 p1;
	return p1;
}
void test03()
{
	//	以值方式返回局部对象
	person3 p = dowork2();
}
int main4_2_3()
{
	//test01();

	//test02();
	test03();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cdbycd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值