C++学习笔记(3):构造函数、析构函数、默认的构造和析构、析构函数调用的顺序、深拷贝与浅拷贝

一、构造和析构
当我们自定义了一个类之后,在程序中创建对象时,类中的相关变量并未被初始化,构造函数可以完成这个任务。
在对象被创建时,构造函数用来初始化对象,构造函数的名称与类名相同,并且构造函数是可以被重载的,而且它没有返回值。
在对象被销毁时,编译器会自动调用该类的析构函数。析构函数没有参数也没有返回值,在一个对象被销毁时自动调用,无论析构函数是否在类中定义都会调用。可以通过析构函数回收申请的空间,防止内存泄漏。另外析构函数无法进行重载。

//功能:构造函数、构造函数的重载、析构函数的使用
#include <iostream>
using namespace std;


class Test{
public:
	//Test类的构造函数
	//在对象被创建的时候,用来初始化对象的函数
	Test(){//无参构造函数
		m_x = 0;
		m_y = 0;
	}
	//构造方式是可以被重载的
	Test(int x, int y){
		m_x = x;
		m_y = y;
		name = (char*)malloc(100);	
		strcpy(name, "zhangsan");
	}

	Test(int x){
		m_x = x;
		m_y = 0;
	}

	void printT(){
		cout << "x = " << m_x << ", y = " << m_y << endl;
	}

	//析构函数和构造函数都没有返回值
	//析构函数没有形参
	~Test(){//通过析构函数回收申请的空间,防止内存泄漏
		cout << "~Test__" << endl;
		if(name != NULL){
			free(name);
			cout << "free succ" <<endl;
		}
	}
private:
	int m_x;
	int m_y;
	char *name;
};

void test1(){
	Test t1(10, 20);
	t1.printT();
	//在一个对象销毁之前,要自定调用析构函数(不管写不写都会触发析构函数)
}
int main(void){
#if 0
	Test t1(10, 20);
	t1.printT();
	//t1.init(10,20);
	
	Test t2(100);
	 t2.printT();

	 Test t3;//调用类的无参构造函数
	 t3.printT();
#endif
	 test1();
	 getchar();

	return 0;
}

如果没有定义构造函数和析构函数,那么在创建类的对象的时候就会默认给一个无参的构造函数和一个默认的析构函数,当我们自己写一个构造函数或者析构函数时,默认的失效。

//功能:默认构造函数和默认析构函数
#include <iostream>
using namespace std;

class Test{
public:
#if 0
	Test(){ //默认的无参构造函数
	}
#endif

	void prinrT(){
		cout << "x = " << m_x << ",y = " << m_y <<endl;	
	}
	//提供一个有参数的构造函数,默认的构造函数就不复存在
	Test(int x, int y){
		m_x = x;
		m_y = y;
	}

#if 0
	~Test(){ //默认的析构函数
	}
#endif
private:
	int m_x;
	int m_y;
};

int main(void){
	Test t1(10,20);  //调用Test无参数构造函数

	return 0;
}

二、拷贝构造函数

//功能:拷贝构造函数与赋值操作函数再类创建中的区别
#include <iostream>
using namespace std;

class Test{
public:
	Test(){
		m_x = 0;
		m_y = 0;
	}

	Test(int x, int y){
		m_x = x;
		m_y = y;
	}

	void PrintT(){
		cout << "x = " << m_x << " , y = " << m_y <<endl; 
	}
#if 1 
	//显示拷贝构造函数
	Test(const Test &another){
		cout << "拷贝构造函数" <<endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}	
#endif
#if 0 
	//会有一个默认的拷贝构造函数
	Test(const Test &another){
		m_x = another.m_x;
		m_y = another.m_y;
	}
#endif
	//=赋值操作符
	void operator=(const Test &another){
		m_x = another.m_x;
		m_y = another.m_y;
	}	
private:
	int m_x;
	int m_y;
};

int main(void){
	
	Test t1(100,200);
	Test t2(t1);
	t2.PrintT();

	//构造函数是对象初始化的时候调用
	Test t3 = t1;//依然是初始化t3的时候调用t3构造函数,依然是调用t3的拷贝构造函数

//	t3 = t1;//调用的不是t3的拷贝构造函数,而是t3的赋值操作符函数

	return 0;
}

三、默认的拷贝构造函数
1)在类中会有默认的无参构造函数,当类中没有显式无参构造、显式有参构造、显示拷贝构造的函数时,默认的无参构造函数就会出现。
2)在类中会有默认的拷贝构造函数,当类中没有显式拷贝构造的函数时,默认的拷贝构造函数就会出现。
3)类中会有默认的析构函数,当类中没有显式的析构函数时,默认的析构函数就会出现。

#include <iostream>
using namespace std;

class A{
public:
#if 0
	A(){
		m_a = 0;
		m_b = 0;
	}
#endif
#if 0
	A(const A &another){
		m_a = another.m_a
		m_b = another.m_b;
	}
#endif
private:
	int m_a;
	int m_b;	
};
//类中  会有默认的无参构造函数:
//      -->当没有任何显示的构造函数(显示无参构,显示有参,显示拷贝构造)时,默认无参构造函数就会出现
//      会有默认的拷贝构造函数:
//      -->当没有 **显示的拷贝构造** 的函数,默认的拷贝构造就会出现
//      会有默认的析构函数
//      -->当没有 **显示的析构函数** 的时候,默认的析构函数就会出现

int main(void){
	A  a;
	A a1(a);

	return 0;
}

四、拷贝构造函数的应用场景
注意对象析构的顺序,具体每一种情况间例子中的六个场景。

//功能:析构函数,拷贝函数在函数调用时的一些顺序
#include <iostream>
using namespace std;

class Test{
public:
	Test(){
		cout << "Test()......" << endl;
		m_x = 0;
		m_y = 0;
	}
    Test(int x, int y){
		cout << "Test(int x, int y)......" << endl;
		m_x = x;
		m_y = y;
	}
	Test(const Test & another){
		cout<<"Test(const & another)......" <<endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void printT(){
		cout << "x = " << m_x << ", y = " << m_y <<endl; 
	}

	void operator=(const Test & another){
		cout<<" operator=(const Test & another)......" <<endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	~Test(){
		cout<<"~Test()......" <<endl;
	}


private:
	int m_x;
	int m_y;
};

//析构函数的顺序,跟构造函数相反,谁先构造,谁后析构
//场景1
void test1(){
	Test t1(10, 20);
	Test t2(t1);//Test t2 = t1;  先析构t2,再析构t1
}
/*
	Test(int x, int y)......
	Test(const & another)......
	~Test()......    
	~Test()......
*/

//场景2
void test2(){
	Test t1(10,20);
	Test t2;

	t2 = t1;//等号操作符
}
/*
	Test(int x, int y)......
	Test()......
	 operator=(const Test & another)......
	~Test()......
	~Test()......
*/

//场景3
void func(Test t){  //Test t = t1 //est t 的拷贝构造函数
	cout << "func begin....." << endl;
	t.printT();
	cout << "func end....." << endl;
}

void test3(){
	cout << "test3 begin....." << endl;
	Test t1(20,30);
	func(t1);
	cout << "test3 end....." << endl;
}
/*
	test3 begin.....
	Test(int x, int y)......
	Test(const & another)......
	func begin.....
	x = 20, y = 30
	func end.....
	~Test()......
	test3 end.....
	~Test()......
*/

//场景4
Test func2(){
	cout << "func2 begin....." << endl;
	Test temp(10,20);
	temp.printT();
	cout << "func2 end....." << endl;
	return temp;
}//匿名的对象 = temp

void test4(){
	cout << "test4 begin....." << endl;
	func2();//返回一个匿名对象。当一个函数返回一个匿名对象时,函数外部没有任何
	        //变量去接收它,这个匿名对象将不会再被使用,(找不到),编译器会直接
			//将这个匿名对象回收掉,而不是等待整个函数执行完毕再回收
	cout << "test4 end....." << endl;
}
/*
	test4 begin.....
	func2 begin.....
	Test(int x, int y)......
	x = 10, y = 20
	func2 end.....
	Test(const & another)......
	~Test()......
	~Test()......
	test4 end.....
*/

//场景五
void test5(){
	cout << "test5 begin....." << endl;
	Test t1 = func2();//并不会出发拷贝构造,而是将匿名对象转正为t1
					 //给匿名对象起名为t1
	cout << "test5 end....." << endl;
}
/*
	test5 begin.....
	func2 begin.....
	Test(int x, int y)......
	x = 10, y = 20
	func2 end.....
	Test(const & another)......
	~Test()......
	test5 end.....
	~Test()......
*/

//场景6
void test6(){
	cout << "test6 begin....." << endl;
	Test t1; //t1已经被初始化了
	t1 = func2();
	t1.printT();
	cout << "test6 end....." << endl;
}
/*
	test6 begin.....
	Test()......
	func2 begin.....
	Test(int x, int y)......
	x = 10, y = 20
	func2 end.....
	Test(const & another)......
	~Test()...... 
	 operator=(const Test & another)......
	~Test()......  //匿名对象的析构
	x = 10, y = 20
	test6 end.....
	~Test()......
*/

int main(void){
	//test1();
	//test2();
	//test3();
	//test4();
	test6();
	getchar();
	return 0;
}

五、深拷贝与浅拷贝

//功能:浅拷贝的例子,该程序不会正常运行
#include<iostream>
using namespace std;

class Teacher
{
public:
	Teacher(int id, char * name){
		
		cout <<"Teacher(int , char*)" <<endl;
		//赋值id
		m_id = id;

		//赋值name
		int len = strlen(name);
		m_name = (char*)malloc(len+1);
		strcpy(m_name, name);
	}

	void printT(){
		cout<< "name = " << m_name << " ,id =  " << m_id <<endl;
	}

	~Teacher(){
		cout << "~Teacher()....." <<endl;
		if(m_name != NULL){
			free(m_name);
			m_name = NULL;		
		}
	}
private:
	int m_id;
	char *m_name;
};

void test(){

	Teacher t1(1,"zhangsan");
	t1.printT();
	Teacher t2(t1); //t2的默认拷贝构造
	t2.printT();
}

int main(void){
	test();
	return 0;
}

运行以上例子,会出现无法运行的错误,因为在调用test函数后当test函数执行完毕后,t1对象执行析构函数,销毁t1对象中m_name指针所指向的内容,但t2对象中m_name指针指向的时同一块区域,当销毁t2指针指向的区域时候,会发现t2指针指向的已经被销毁了,故会发生错误。这是浅拷贝的一种。

//功能:深拷贝的例子
#include<iostream>
using namespace std;

class Teacher
{
public:
	Teacher(int id, char * name){
		
		cout <<"Teacher(int , char*)" <<endl;
		//赋值id
		m_id = id;

		//赋值name
		int len = strlen(name);
		m_name = (char*)malloc(len+1);
		strcpy(m_name, name);
	}

	void printT(){
		cout<< "name = " << m_name << " ,id =  " << m_id <<endl;
	}

	//提供一个显示拷贝构造函数,来完成深拷贝动作
	Teacher(const Teacher &another){
		m_id = another.m_id;
		
		//深拷贝动作
		int len = strlen(another.m_name);
		m_name = (char *)malloc(len+1);
		strcpy(m_name, another.m_name);
	}

	~Teacher(){
		cout << "~Teacher()....." <<endl;
		if(m_name != NULL){
			free(m_name);
			m_name = NULL;		
		}
	}
private:
	int m_id;
	char *m_name;
};

void test(){

	Teacher t1(1,"zhangsan");
	t1.printT();
	Teacher t2(t1); //t2的默认拷贝构造
	t2.printT();
}

int main(void){
	//增加显示拷贝的函数后可以正常运行
	test();
	return 0;
}

当在类中加入显示拷贝函数,t1与t2就会指向两个不同的空间,为深拷贝。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值