C++浅拷贝与深拷贝

文章目录

  • 1.浅拷贝
  • 2.深拷贝
  • 浅拷贝
  • 深拷贝

1.浅拷贝

#include 
using namespace std;

/*
浅拷贝(shallow copy):指针类型的变量只会拷贝地址值
深拷贝(deep copy):将指针指向的内容拷贝到新的存储空间
*/

class Car {
	int m_price;
	char *m_name;
	//void copy(const char *name = NULL) {
	//	if (name == NULL) return;

	//	// 申请新的堆空间
	//	m_name = new char[strlen(name) + 1] {};
	//	// 拷贝字符串数据到新的堆空间
	//	strcpy(m_name, name);
	//}
public:
	Car(int price = 0, char* name = NULL) : m_price(price), m_name(name) {}
	/*	copy(name);
	}*/

	//Car(const Car &car) :m_price(car.m_price) {
	//	copy(car.m_name);
	//}

	//~Car() {
	//	if (m_name == NULL) return;

	//	delete[] m_name;
	//	m_name = NULL;
	//}

	void display() {
		cout << "price is " << m_price << ", name is " << m_name << endl;
	}
};

//class Car {
//	int m_price;
//	char *m_name;
//};

//class Person {
//	int m_age;
//	Car m_car;
//};

Car* g_car;

void test() {
	char name[] = { 'b', 'm', 'w', '\0' };
	
	g_car = new Car(100, name);
}

int main() {
	//Person person1;

	//Person person2 = person1;


	/*Car car1(100, "bmw");
	Car car2 = car1;
	car2.display();*/

	// const char *name = "bmw";

	// char name[] = {'b', 'm', 'w', '\0'};

	// char *name = new char[4] {'b', 'm', 'w', '\0'};

	/*Car *car = new Car(100, "bmw");
	car->display();*/

	// delete[] name;

	test();

	g_car->display();

	getchar();
	return 0;
}

C++浅拷贝与深拷贝_第1张图片

2.深拷贝

#include 
using namespace std;

/*
浅拷贝(shallow copy):指针类型的变量只会拷贝地址值
深拷贝(deep copy):将指针指向的内容拷贝到新的存储空间
*/

class Car {
	int m_price;
	char *m_name;
	//void copy(const char *name = NULL) {
	//	if (name == NULL) return;

	//	// 申请新的堆空间
	//	m_name = new char[strlen(name) + 1] {};
	//	// 拷贝字符串数据到新的堆空间
	//	strcpy(m_name, name);
	//}
public:
	Car(int price = 0, const char *name = NULL) : m_price(price) {
		if (name == NULL) return;
		// 申请新的堆空间
		m_name = new char[strlen(name) + 1] {};
		// 拷贝字符串数据到新的堆空间
		strcpy(m_name, name);
	}
	/*	copy(name);
	}*/

	//Car(const Car &car) :m_price(car.m_price) {
	//	copy(car.m_name);
	//}

	~Car() {
		if (m_name == NULL) return;

		delete[] m_name;
		m_name = NULL;
	}

	void display() {
		cout << "price is " << m_price << ", name is " << m_name << endl;
	}
};

//class Car {
//	int m_price;
//	char *m_name;
//};

//class Person {
//	int m_age;
//	Car m_car;
//};

int main() {
	//Person person1;

	//Person person2 = person1;


	/*Car car1(100, "bmw");
	Car car2 = car1;
	car2.display();*/

	// const char *name = "bmw";

	// char name[] = {'b', 'm', 'w', '\0'};

	// char *name = new char[4] {'b', 'm', 'w', '\0'};

	Car *car = new Car(100, "bmw");
	car->display();

	// delete[] name;

	//test();

	//g_car->display();

	getchar();
	return 0;
}

C++浅拷贝与深拷贝_第2张图片

浅拷贝

编译器默认提供的拷贝是浅拷贝(shallow copy):

  • 将一个对象中所有成员变量的值拷贝到另一个对象
  • 如果某个成员变量是个指针,只会拷贝指针中存储的地址值,并不会拷贝指针指向的内存空间
  • 可能会导致堆空间多次 free 的问题
#include 
using namespace std;

/*
浅拷贝(shallow copy):指针类型的变量只会拷贝地址值
深拷贝(deep copy):将指针指向的内容拷贝到新的存储空间
*/

class Car {
	int m_price;
	char *m_name;
	//void copy(const char *name = NULL) {
	//	if (name == NULL) return;

	//	// 申请新的堆空间
	//	m_name = new char[strlen(name) + 1] {};
	//	// 拷贝字符串数据到新的堆空间
	//	strcpy(m_name, name);
	//}
public:
	Car(int price = 0, const char *name = NULL) : m_price(price) {
		if (name == NULL) return;
		// 申请新的堆空间
		m_name = new char[strlen(name) + 1] {};
		// 拷贝字符串数据到新的堆空间
		strcpy(m_name, name);
	}
	/*	copy(name);
	}*/

	//Car(const Car &car) :m_price(car.m_price) {
	//	copy(car.m_name);
	//}

	~Car() {
		if (m_name == NULL) return;

		delete[] m_name;
		m_name = NULL;
	}

	void display() {
		cout << "price is " << m_price << ", name is " << m_name << endl;
	}
};

//class Car {
//	int m_price;
//	char *m_name;
//};

//class Person {
//	int m_age;
//	Car m_car;
//};

int main() {
	//Person person1;

	//Person person2 = person1;


	Car car1(100, "bmw");
	Car car2 = car1;
	car2.display();

	// const char *name = "bmw";

	// char name[] = {'b', 'm', 'w', '\0'};

	// char *name = new char[4] {'b', 'm', 'w', '\0'};

	//Car *car = new Car(100, "bmw");
	//car->display();

	// delete[] name;

	//test();

	//g_car->display();

	getchar();
	return 0;
}

C++浅拷贝与深拷贝_第3张图片

深拷贝

如果需要实现深拷贝(deep copy),就需要自定义拷贝构造函数。

将指针类型的成员变量所指向的内存空间,拷贝到新的内存空间。

#include 
using namespace std;

/*
浅拷贝(shallow copy):指针类型的变量只会拷贝地址值
深拷贝(deep copy):将指针指向的内容拷贝到新的存储空间
*/

class Car {
	int m_price;
	char *m_name;
	void copy(const char *name = NULL) {
		if (name == NULL) return;
		// 申请新的堆空间
		m_name = new char[strlen(name) + 1] {};
		// 拷贝字符串数据到新的堆空间
		strcpy(m_name, name);
	}
public:
	Car(int price = 0, const char *name = NULL) : m_price(price) {
		copy(name);
	}

	Car(const Car &car) : m_price(car.m_price) {
		copy(car.m_name);
	}

	~Car() {
		if (m_name == NULL) return;

		delete[] m_name;
		m_name = NULL;
	}

	void display() {
		cout << "price is " << m_price << ", name is " << m_name << endl;
	}
};

//class Car {
//	int m_price;
//	char *m_name;
//};

//class Person {
//	int m_age;
//	Car m_car;
//};

int main() {
	//Person person1;

	//Person person2 = person1;

	Car car1(100, "bmw");
	Car car2 = car1;
	car2.display();

	// const char *name = "bmw";

	// char name[] = {'b', 'm', 'w', '\0'};

	// char *name = new char[4] {'b', 'm', 'w', '\0'};

	//Car *car = new Car(100, "bmw");
	//car->display();

	// delete[] name;

	//test();

	//g_car->display();

	getchar();
	return 0;
}

C++浅拷贝与深拷贝_第4张图片

你可能感兴趣的:(重学C++,c++)