拷贝构造函数

拷贝构造函数
一,合成的拷贝构造函数
二,自定义的拷贝构造函数

一,合成的拷贝构造函数

#include <iostream>
#include <string>

using namespace std;

#define ADDR_LEN 64

//定义“人类”
class Human {
public: 
	Human(); //构造函数,可以写在类里面(由内联函数的效果),也可以写在外面
	Human(int age, int salary);

	void eat(); //类里面的方法,又称为成员函数
	void sleep();
	void work();
	void play();
	void description();
	void setAddr(const char* addr);

	string getName();
	int getAge();
	int getSalary();

private:
	//类内初始值,C++11以上的版本才支持
	//在创建C++对象的时候,一开始就设定一个初始值,仅适用于特殊场合
	string name = "牛少侠";
	int age = 23;
	int salary = 10000;

	char* addr;
};

void Human::eat() {
	cout << "吃鸡屁股, 喝可乐!" << endl;
}

void Human::sleep() {
	cout << "夜深了!上床睡觉!" << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

void Human::play() {
	cout << "玩奇幻角色扮演!" << endl;
}

string Human::getName() {
	return name;
}

void Human::description() {
	cout << "name: " << name
		<< "  age: "   << age
		<< "  salary: " << salary 
		<< "地址:" << addr << endl;

	cout << (int)addr << endl;
}

void Human::setAddr(const char* addr) {
	if (!addr){
		return;
	}

	strcpy_s(this->addr, ADDR_LEN, addr);
	cout << (int)this->addr << endl;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

//没有设置类内初始值时,进行手动构造函数
Human::Human() {
	name = "无名";
	age = 18;
	salary = 30000;

	addr = new char[64];
	strcpy_s(this->addr, ADDR_LEN, "China");
}

Human::Human(int age, int salary) {
	cout << "执行调用了自定义的构造函数:Human(int age, int salary)!" << endl;

	//指针this指向类的对象
	this->age = age;
	this->salary = salary;
	this->name = "天杀的";

	addr = new char[64];
	strcpy_s(this->addr, ADDR_LEN, "China");
}

int main(void) {
	Human b1(25,28000);

	//调用拷贝构造函数
	//编译器会自动合成调用拷贝构造函数,但是编译器自动合成的拷贝构造函数很危险,会出问题
	//所以需要自定义一个拷贝构造函数
	Human b2 = b1; 
	Human b3(b1);  //调用拷贝构造函数

	b1.description();
	b2.description();
	b3.description();

	b1.setAddr("美国");

	cout << "b1地址修改之后" << endl;
	b1.description();
	b2.description();
	b3.description();

	return 0;
}

输出为:
在这里插入图片描述
根据输出可以就看出,无论是对象b1,对象b2,对象b3,里面的指针变量addr中的地址的值,始终都相同,没有改变。
但是地址指向的内容,从“China”改成了“美国”

二,自定义的拷贝构造函数

#include <iostream>
#include <string>

using namespace std;

#define ADDR_LEN 64

//定义“人类”
class Human {
public: 
	Human(); //构造函数,可以写在类里面(由内联函数的效果),也可以写在外面
	Human(int age, int salary);

	//标准的拷贝构造函数是一个引用类型,别名后面的参数other可以省略
	Human(const Human &other); 

	void eat(); //类里面的方法,又称为成员函数
	void sleep();
	void work();
	void play();
	void description();
	void setAddr(const char* addr);

	string getName();
	int getAge();
	int getSalary();

private:
	//类内初始值,C++11以上的版本才支持
	//在创建C++对象的时候,一开始就设定一个初始值,仅适用于特殊场合
	string name = "牛少侠";
	int age = 23;
	int salary = 10000;

	char* addr;
};

void Human::eat() {
	cout << "吃鸡屁股, 喝可乐!" << endl;
}

void Human::sleep() {
	cout << "夜深了!上床睡觉!" << endl;
}

void Human::work() {
	cout << "我在工作..." << endl;
}

void Human::play() {
	cout << "玩奇幻角色扮演!" << endl;
}

string Human::getName() {
	return name;
}

void Human::description() {
	cout << "name: " << name
		<< "  age: "   << age
		<< "  salary: " << salary 
		<< "地址:" << addr << endl;

	cout << (int)addr << endl;
}

void Human::setAddr(const char* addr) {
	if (!addr){
		return;
	}

	strcpy_s(this->addr, ADDR_LEN, addr);
	cout << (int)this->addr << endl;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

//没有设置类内初始值时,进行手动构造函数
Human::Human() {
	name = "无名";
	age = 18;
	salary = 30000;

	addr = new char[64];
	strcpy_s(this->addr, ADDR_LEN, "China");
}

Human::Human(int age, int salary) {
	cout << "执行调用了自定义的构造函数:Human(int age, int salary)!" << endl;

	//指针this指向类的对象
	this->age = age;
	this->salary = salary;
	this->name = "天杀的";

	addr = new char[64];
	strcpy_s(this->addr, ADDR_LEN, "China");
}

//拷贝构造函数
//相当于const Human& other = b1;
//一个类中只能定义一个拷贝构造函数,不能有其他重载形式
Human::Human(const Human& other) { 
	cout << "执行调用了拷贝构造函数:Human(const Human& other)!" << endl;

	name = other.name;
	age = other.age;
	salary = other.salary;

	//在堆空间独立分配一块内存,用来存储字符串常量China
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, "China");
}

int main(void) {
	Human b1(25,28000);

	//调用拷贝构造函数
	//编译器会自动合成调用拷贝构造函数,但是编译器自动合成的拷贝构造函数很危险,会出问题
	//所以需要自定义一个拷贝构造函数
	Human b2 = b1; 
	Human b3(b1);  //调用拷贝构造函数

	b1.description();
	b2.description();
	b3.description();

	b1.setAddr("美国");

	cout << "b1地址修改之后" << endl;
	b1.description();
	b2.description();
	b3.description();

	return 0;
}

输出为:
在这里插入图片描述
根据输出可以就看出,在对象b1地址修改前后,无论是对象b1,对象b2,对象b3,里面的指针变量addr中的值,始终都相同,没有改变。
但是对象b1地址指向的内容,从“China”改成了“美国”;
对象b2,对象b3地址指向的内容始终都相同,前后都是“China”,没有改变内容

综合来看:
编译器自动合成的拷贝函数,拷贝的是指针变量里面的地址的值,而不是指针所指向的内容;
而自定义的拷贝构造函数,在堆空间中,给对象b1,对象b2,对象b3重新分配了一块对立的内存,拷贝的是指针指向的内容,而不是指针本身的地址!

那么,什么时候调用拷贝构造函数?
请参考另一篇博文:什么时候调用拷贝构造函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值