构造函数调用规则


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

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

造函数调用规则如下:

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

如果用户定义拷贝构造函数,c++不会再提供其他构造函数


示例:

#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
	int m_age;
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person的默认析构函数" << endl;
	}
	Person(int age)
	{
		m_age = age;
		cout << "Person的有参构造函数调用" << endl;
	}
	Person(const Person& p)
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}
	
};

void test01()
{
	Person p;
	p.m_age = 81;
	Person p1(p);
	cout << p1.m_age << endl;
}
int main()
{

	test01();
	system("pause");
	return 0;
}

很完美的一个例程代码,其输出结果为:

 但是我们如果把拷贝构造函数注释掉之后,这时p的年龄又会是多少呢?

#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
	int m_age;
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person的默认析构函数" << endl;
	}
	Person(int age)
	{
		m_age = age;
		cout << "Person的有参构造函数调用" << endl;
	}
	/*Person(const Person& p)
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}*/
	
};

void test01()
{
	Person p;
	p.m_age = 81;
	Person p1(p);
	cout << p1.m_age << endl;
}
int main()
{

	test01();
	system("pause");
	return 0;
}

我们 会发现p的年龄还是81没有发生变化的。但是输出也有部分变化,比如后面的那个就没有输出”拷贝构造函数的调用“,为什么?

因为我们给拷贝构造函数注释掉了,在默认拷贝构造函数里面只有值的拷贝,没有输出的那句话,所以值是系统给默认拷贝过来了的二文字没有输出。

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

假设代码如下:

#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
	int m_age;//默认构造函数被我们注释掉了
	/*Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}*/
	~Person()
	{
		cout << "Person的默认析构函数" << endl;
	}
	Person(int age)
	{
		m_age = age;
		cout << "Person的有参构造函数调用" << endl;
	}
	//Person(const Person& p)
	//{
	//	cout << "Person的拷贝构造函数调用" << endl;
	//	m_age = p.m_age;
	//}
};

void test01()
{
	Person p;
	p.m_age = 81;
	Person p1(p);
	cout << p1.m_age << endl;
}
void test02()
{
	Person p;


}
int main()
{

//	test01();
	test02();

	system("pause");
	return 0;
}

 编译会出现错误(因为我们如果设置了有参构造函数之后,系统就不会创建默认构造函数)

 

#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
	int m_age;
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person的默认析构函数" << endl;
	}
	Person(int age)
	{
		m_age = age;
		cout << "Person的有参构造函数调用" << endl;
	}
	//Person(const Person& p)
	//{
	//	cout << "Person的拷贝构造函数调用" << endl;
	//	m_age = p.m_age;
	//}
};

void test01()
{
	Person p;
	p.m_age = 81;
	Person p1(p);
	cout << p1.m_age << endl;
}
void test02()
{
	Person p(28);
	Person p1(p);
	cout << p1.m_age << endl;


}
int main()
{

//	test01();
	test02();

	system("pause");
	return 0;
}

其输出结果为:

 验证了就算我们创建了有参构造函数,系统也会提供默认拷贝构函数。

2.如果用户定义拷贝构造函数,c++不会再提供其他构造函数

同样的代码,我们将系统默认构造函数和有参构造函数给注释掉

#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
	int m_age;
	/*Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person的默认析构函数" << endl;
	}
	Person(int age)
	{
		m_age = age;
		cout << "Person的有参构造函数调用" << endl;
	}*/
	Person(const Person& p)
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}
};

void test01()
{
	Person p;
	p.m_age = 81;
	Person p1(p);
	cout << p1.m_age << endl;
}
void test02()
{
	Person p(28);
	Person p1(p);
	cout << p1.m_age << endl;


}
int main()
{

//	test01();
	test02();

	system("pause");
	return 0;
}

 系统不会提供注释掉的两个函数。

总结,如果我们定义了有参构造函数系统会提供默认拷贝构造函数而不会提供默认构造函数,但如果我们定义了拷贝构造函数,系统就不会创建默认构造函数和有参构造函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大家好我是覃同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值