什么时候调用拷贝构造函数

什么时候调用拷贝构造函数:

1.调用函数时,实参是对象,形参不是引用类型
如果函数的形参是引用类型,就不会调用拷贝构造函数

2.函数的返回类型是类,而且不是引用类型

3.对象数组的初始化列表中,使用对象。

第一种 调用函数时,实参是对象,形参不是引用类型

#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() const;
	void setAddr(const char* addr);

	string getName() const; //加const之后就不能在里面修改里面的值
	int getAge();
	int getSalary() const;

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() const { //加const之后就不能在里面修改里面的值
	return name;
}

void Human::description() const{
	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() const{
	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;

	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, "China");
}

void showMsg(const Human man) {
	cout << man.getName() << "的基本信息:" << endl;
	man.description();
}

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

	showMsg(b1); 

	return 0;
}

输出为:
在这里插入图片描述
传递参数的时候,b1拷贝到man,调用了一次拷贝函数!

也可以传递一个引用:

void showMsg(const Human &man) {
	cout << man.getName() << "的基本信息:" << endl;
	man.description();
}

输出为:
在这里插入图片描述
如果函数的形参是引用类型,就不会调用拷贝构造函数!

使用引用时,在参数和返回类型中,使用const,作用是只读。防止被别人搞事情,修改类Human。此时,类中的方法,和方法的实现后面要加上const,例如:void getAge() const;
在一般的小项目,小程序中,可以不用这么精细!

第二种 函数的返回类型是类,而且不是引用类型
代码描述:怎么找到一个更好的男人:比较两个男人的薪水,谁的薪水高选择谁

这里有几种写法:
1.

//怎么找到一个更好的男人:比较两个男人的薪水,谁的薪水高选择谁
const Human getBetterMan(const Human man1, const Human man2) {
	if (man1.getSalary() > man2.getSalary()) {
		return man1;
	}
	else {
		return man2;
	}
}

int main(void) {
	Human b1(25, 28000);
	Human b2(26, 30000);
	
	//返回一个临时对象,为了构建这个函数,编译器调用了拷贝构造函数
	getBetterMan(b1, b2); 
	
	return 0;
}

输出为:
在这里插入图片描述
传递参数的时候,b1拷贝到man1,b1拷贝到man1,函数返回的时候,并不是返回man1或者man2,而是两者中的一个复制品。因此,一共拷贝了三次,拷贝构造函数被调用了三次

//怎么找到一个更好的男人:比较两个男人的薪水,谁的薪水高选择谁
const Human getBetterMan(const Human &man1, const Human &man2) {
	if (man1.getSalary() > man2.getSalary()) {
		return man1;
	}
	else {
		return man2;
	}
}
```cpp
//怎么找到一个更好的男人:比较两个男人的薪水,谁的薪水高选择谁
const Human &getBetterMan(const Human &man1, const Human &man2) {
	if (man1.getSalary() > man2.getSalary()) {
		return man1;
	}
	else {
		return man2;
	}
}

int main(void) {
	Human b1(25, 28000);
	Human b2(26, 30000);
	
	getBetterMan(b1, b2);
	
	return 0;
}

输出为:
在这里插入图片描述
返回的是一个引用,传递的参数也是引用。因此,并没有调用拷贝构造函数!

第三种 对象数组的初始化列表中,使用对象

int main(void) {
	Human f1, f2, f3, f4;
	Human F4[4] = { f1, f2, f3, f4 };
	
	return 0;
}

输出为:
在这里插入图片描述
调用了四个拷贝构造函数!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值