类和对象-对象的初始化和清理-构造函数调用规则

默认情况下,c++编译器至少给一个类添加3个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则如下:

  1. 如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
  2. 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
#include<iostream>
using namespace std;

//构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)

//2、
//如果我们写了有参构造函数,编译器就不再提供默认构造,依然会提供拷贝构造
//如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数

class Person {
public:

	/*Person() {
		cout << "Person的默认构造函数调用" << endl;
	}*/

	/*Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}*/

	Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

//void test01() {
//	Person p1;
//	p1.m_Age = 18;
//
//	Person p2(p1);
//
//	cout << "p2的年龄为:" << p2.m_Age << endl;
//}

void test02() {
	//Person p3(28);
	
	//Person p4(p3);

	//cout << "p4的年龄为:" << p4.m_Age << endl;
}

int main() {

	//test01();
	//test02();

	system("pause");

	return 0;
}

默认情况下,c++编译器至少给一个类添加3个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
class Person {
public:

	Person() {
		cout << "Person的默认构造函数调用" << endl;
	}

	Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

void test01() {
	Person p1;
	p1.m_Age = 18;

	Person p2(p1);

	cout << "p2的年龄为:" << p2.m_Age << endl;
}

int main(){
	test01();

	return 0;
}

运行结果
在这里插入图片描述
把拷贝构造函数注释掉

class Person {
public:

	Person() {
		cout << "Person的默认构造函数调用" << endl;
	}

	Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	/*Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}*/

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

void test01() {
	Person p1;
	p1.m_Age = 18;

	Person p2(p1);

	cout << "p2的年龄为:" << p2.m_Age << endl;
}

int main(){
	test01();

	return 0;
}

运行结果
在这里插入图片描述
p2依然显示年龄,并未有Person的拷贝构造函数,并未报错,因此可知编译器提供了默认拷贝构造函数,即m_Age = age;

构造函数调用规则如下:

  1. 如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
  2. 如果用户定义拷贝构造函数,c++不会再提供其他构造函数

1.如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造

//提供有参构造函数,但不提供默认构造函数
class Person {
public:

	/*Person() {
		cout << "Person的默认构造函数调用" << endl;
	}*/

	Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	/*Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}*/

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

void test02(){
	Person p3;
}

int main(){
	test02();

	return 0;
}

此时会报错
在这里插入图片描述
因为我们提供了有参构造函数,所有编译器不再提供默认构造函数,所以会出现报错

//提供有参构造函数,但不提供拷贝构造函数
class Person {
public:

	/*Person() {
		cout << "Person的默认构造函数调用" << endl;
	}*/

	Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	/*Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}*/

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

void test02(){
	Person p3(28);
	
	Person p4(p3);

	cout<< "p4的年龄为:" << p4.m_Age << endl;
}

int main(){
	test02();

	return 0;
}

运行结果
在这里插入图片描述
自己提供有参构造函数,即使我们不提供拷贝构造函数,编译器也会提供默认拷贝构造函数

  1. 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
//提供拷贝构造函数,但不提供普通构造函数
class Person {
public:

	/*Person() {
		cout << "Person的默认构造函数调用" << endl;
	}*/

	/*Person(int age) {
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}*/

	Person(const Person& p) {
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}

	~Person() {
		cout << "Person的析构函数调用" << endl;
	}


	int m_Age;
};

void test02(){
	Person p3;
}

int main(){
	test02();

	return 0;
}

此时会报错
在这里插入图片描述

因为我们提供了拷贝构造函数,但并没有提供默认构造函数,编译器也不会提供默认构造函数,因此会报错。

总结:写了有参构造,编译器不会提供默认构造,会提供拷贝构造;写了拷贝构造,不会提供默认构造和有参构造。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值