20240415,构造函数和析构函数,拷贝构造函数调用时机&规则

目录

二,对象的初始化和清理

2.1 构造函数和析构函数

2.2 函数分类及调用

2.3 拷贝构造函数调用时机

2.4 构造函数调用规则

二,对象的初始化和清理

2.1 构造函数和析构函数

解决初始化和清理问题,编译器自动调用,如果不提供,编译器提供,但空实现 
构造函数类名(){ };没用返回值也不写VOID,函数名称和类名称相同,可以有参数,可以发生重载,自动调用且只一次调用构造
析构函数:~类名 ( ){ };两点特性同上,无参数且不可重载,程序在对象销毁前自动且仅一次调用析构

#include<iostream>
using namespace std;
//对象的初始化和清理
//构造··,无返,类名,有参,自动
//析构··,无返,类名,无参,自动
class Person
{
public:
	Person()
	{
		cout << "Person 构造函数的调用" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}
};
void test01()//创建对象,但是没有调用类的函数
{
	Person p;//局部变量,栈上的数据,TEST01执行完毕后,释放这个对象
}
int main()
{
	test01();//
	cout << endl;
	static Person p1;
	
	system("pause");//PAUSE是暂停的意思,此时MAIN函数没有结束,P1没有被释放
	return 0;//结束main函数,返回0
}
2.2 函数分类及调用

分类::参数:有参&无参构造;类型:普通&拷贝构造
调用方式::括号,显示,隐式转换

#include<iostream>
using namespace std;
class Person
{
public:
	//普通
	Person()//无参,默认
	{
		cout << "Person 无参构造函数的调用" << endl;
	}
	Person(int a)
	{
		age = a;
		cout << "Person 有参构造函数的调用" << endl;
	}
	//拷贝
	Person(const Person& p)//将传入人的所有属性拷贝到我身上?
	{
		age = p.age;
		cout << "Person 拷贝构造函数的调用" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}
	int age;
};
//调用
void test01()
{
	//括号
	Person p1;//默认无参     不能加括号,PERSON P1();会被认为是一个函数的声明,不会被认为在创建对象
	Person p2(10);//有参
	Person p3(p2);//拷贝
	//函数结束一起释放
	cout << "P2年龄是" << p2.age << endl;
	cout << "P3年龄是" << p3.age << endl;
}
void test02()
{
	//显示法
	Person p1;
	Person p2 = Person(10);//有参构造调用
	Person p3 = Person(p2);//拷贝··    
	Person(10);//匿名构造,当前行执行结束后,系统会立即回收匿名对象
	cout << "aaa" << endl;
	//Person(p3);
	//不要用拷贝构造函数,初始化对象,编译器认为  Person (p3)==Person p3;一个对象声明
}
void test03()
{
	//隐式转换法
	Person p1 = 10;//Person p1=Person(10)有参
	Person p2 = p1;//拷贝
}
int main()
{
	test01();
	cout << endl;
	test02();
	cout << endl;
	test03();
	system("pause");
	return 0;
}
2.3 拷贝构造函数调用时机

使用一个已经创建完毕的对象来初始化一个新对象
值传递的方式给函数参数传值
值方式返回局部对象(有点问题,老师讲的和我的结果有出入,没没有调用成功)-我的编译是将函数返回的PERSON当作一个常量值,而不是一个PERSON

#include<iostream>
using namespace std;
class Person
{
public:
	Person()
	{
		cout << "Person 默认构造函数的调用" << endl;
	}
	Person(int a)
	{
		cout << "Person 有参构造函数的调用" << endl;
		age = a;
	}
	Person(const Person& p)
	{
		cout << "Person 拷贝构造函数的调用" << endl;
		age = p.age;
	}
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}
	int age;
};

void test01()//使用一个已经创建完毕的对象来初始化一个新对象
{
	Person p1(20);
	Person p2(p1);
	cout << "p2的年龄" << p2.age << endl;
}
void dowork(Person p)
{
	
}
void test02()//值传递的方式给函数参数传值
{
	Person p;
	dowork(p);//P对象值传入函数,Person p=p;(隐式转换法,乐)产生形参的时候构造析构了一下?
}
Person dowork2()
{
	Person p;
	cout << (int*) & p << endl;//打印地址
	return p;
}
void test03()//值方式返回局部对象
{
	Person p(dowork2());//只调用了一次?
	cout << (int*)&p << endl;
	Person p1 = dowork2();//还是一次
	//以上都没用触发拷贝函数,而是默认函数,dowork2()虽然是返回一个Person,但是算值????
	cout << (int*)&p1 << endl;
	cout << endl;
	Person p2(p);
	//前两个地址相同,P2的地址不同,乐
	cout << (int*)&p2 << endl;

}
int main()
{
	test01();
	cout << endl;
	test02();
	cout << endl;
	test03();
	system("pause");
	return 0;
}
2.4 构造函数调用规则

默认情况下,C++编译器至少给一个类添加三个函数:默认构造(无参,函数体为空);默认析构(无参,函数体为空);默认拷贝构造,对属性进行值拷贝

规则:如果用户定义有参构造函数,C++不再提供默认无参构造,但会提供默认拷贝构造
如果用户定义拷贝构造函数,C++不会再提供其他构造函数

#include<iostream>
using namespace std;

class Person
{
public:
	//Person()//运行test01不能注释
	如果用户定义有参构造函数,C++不再提供默认无参构造,但会提供默认拷贝构造
	//{
	//	cout << "Person 默认构造函数的调用" << endl;
	//}
	Person(int a)
	{
		cout << "Person 有参构造函数的调用" << endl;
		age = a;
	}
	/*Person(const Person& p)
	{
		cout << "Person 拷贝构造函数的调用" << endl;
		age = p.age;
	}*/
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}
	int age;
};

//void test01()
//{
//	Person p;
//	p.age = 18;
//	Person p1(p);
//	cout << "p1的年龄" << p1.age << endl;
//}
void test02()//运行test02可以注释,
{
	Person p(20);
	Person p1(p);
	cout << "p1的年龄" << p1.age << endl;
}
int main()
{
	//test01();
	cout << endl;
	test02();
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;

class Person
{
public:
	/*Person()
	{
		cout << "Person 默认构造函数的调用" << endl;
	}
	Person(int a)
	{
		cout << "Person 有参构造函数的调用" << endl;
		age = a;
	}*/
	Person(const Person& p)
	{
		cout << "Person 拷贝构造函数的调用" << endl;
		age = p.age;
	}
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}
	int age;
};

void test03()//如果用户定义拷贝构造函数,C++不会再提供其他构造函数
{
	Person p;
	Person p(10);
}
int main()
{
	test03();
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值