C++复习之构造函数调用规则

1、创建一个类,C++会给每个类添加至少3个函数(默认(空实现),析构(空实现),拷贝构造(值拷贝)) 

#include<iostream>
using namespace std;


 class Person
 {
 	
 	public:
	 int m_age;
// 		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;
 			
		 }
		 
 };
 


void test01()
{
	Person p;
	p.m_age=24;
	Person p2(p);
	 cout<<"p2的年龄:"<<p2.m_age<<endl;

}

int main()
{

	test01();

	return 0;
}
 

2、如果写了有参构造函数,编译器就不会提供无参构造函数 ,但依然提供拷贝构造函数

以下验证写了有参,既无无参:

#include<iostream>
using namespace std;


 class Person
 {
 	
 	public:
	 int m_age;
// 		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;
 			
		 }
		 
 };
 

void test02()
{
	Person p1;

}


//void test01()
//{
//	Person p;
//	p.m_age=24;
//	Person p2(p);
//	 cout<<"p2的年龄:"<<p2.m_age<<endl;
//
//}


int main()
{

//	test01();

	test02();

	return 0;
}
 
 

以下验证写了有参,仍有拷贝:

#include<iostream>
using namespace std;


 class Person
 {
 	
 	public:
	 int m_age;
// 		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;
 			
		 }
		 
 };
 

void test03()
{
	Person p(30);
	Person p3(p);
	cout<<"p3的年龄:"<<p3.m_age<<endl;
	
} 

int main()
{

	test03();
	return 0;
}
 

3、如果写了拷贝构造函数,编译器就不会提供其他的普通构造函数 

#include<iostream>
using namespace std;


 class Person
 {
 	
 	public:
	 int m_age;
// 		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;
 			
		 }
		 
 };
 

void test03()
{
	Person p(30);
	Person p3(p);
	cout<<"p3的年龄:"<<p3.m_age<<endl;
	
} 

int main()
{
	test03();
	return 0;
}
 

相关推荐

  1. C++】编程规范函数规则

    2023-12-29 16:36:02       39 阅读
  2. C++构造函数和析构函数

    2023-12-29 16:36:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 16:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 16:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 16:36:02       82 阅读
  4. Python语言-面向对象

    2023-12-29 16:36:02       91 阅读

热门阅读

  1. docker常用组合命令

    2023-12-29 16:36:02       48 阅读
  2. ::符号的使用

    2023-12-29 16:36:02       52 阅读
  3. 前端实现断点续传文件

    2023-12-29 16:36:02       49 阅读
  4. 2023年12月29日 晨会报告

    2023-12-29 16:36:02       47 阅读
  5. Bagging算法_随机森林Random_Forest

    2023-12-29 16:36:02       45 阅读
  6. Lambda表达式

    2023-12-29 16:36:02       50 阅读