拷贝函数调用时机

C++中拷贝构造函数调用时机通常有三种情况

1.使用一个已经创建完毕的对象来初始化一个新对象

2.值传递的方式给函数参数传值

3.以值方式返回局部对象

#include<iostream>
using namespace std;

//1.使用一个已经创建完毕的对象来初始化一个新对象

class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}

	Person(int age)
	{
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	Person(const Person &p)//温故拷贝构造的写法
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}
	 
	~Person()
	{
		cout << "Person析构函数调用" << endl;
	}
	 
	int m_Age;
};


void test1()
{
	Person p1(20);
	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << endl;
}

int main()
{
	test1();

	return 0;
}
out:
Person的有参构造函数调用
Person的拷贝构造函数调用
p2的年龄为:20
Person析构函数调用
Person析构函数调用
#include<iostream>
using namespace std;

//2.值传递的方式给函数参数传值

class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}

	Person(int age)
	{
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	Person(const Person &p)//温故拷贝构造的写法
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}
	 
	~Person()
	{
		cout << "Person析构函数调用" << endl;
	}
	 
	int m_Age;
};

void doWork1(Person p)
{

}

void test1()
{
	Person p;
	doWork(p);//值传递的方法--本质:会拷贝出一个临时的副本,即调用拷贝构造来创建一个新的东西
}
int main()
{
	test1();

	return 0;
}
out:
Person的默认构造函数调用
Person的拷贝构造函数调用
Person析构函数调用
Person析构函数调用
#include<iostream>
using namespace std;

//3.以值方式返回局部对象

class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}

	Person(int age)
	{
		cout << "Person的有参构造函数调用" << endl;
		m_Age = age;
	}

	Person(const Person &p)//温故拷贝构造的写法
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}
	 
	~Person()
	{
		cout << "Person析构函数调用" << endl;
	}
	 
	int m_Age;
};


Person doWork()
{
	Person p1;//局部对象
	cout << (int*)&p1 << endl;//地址为:00EFF830
	return Person(p1);//由于是以值的方式返回,并不会返回上面的p1,而是重新拷贝一个数据返回
}

void test1()
{
	Person p = doWork();//此处接收的p和上面返回的p1不是同一个对象
	cout << (int*)&p << endl;//地址为:00EFF928
}

int main()
{
	test1();

	return 0;
}
out:
Person的默认构造函数调用
00EFF830
Person的拷贝构造函数调用
Person析构函数调用
00EFF928
Person析构函数调用

注意:值的方式进行返回,会临时创建一个新的数据进行返回,而不是原本的数据(与值传递一致)

相关推荐

  1. 拷贝函数调用时机

    2024-03-22 01:38:01       48 阅读
  2. .__deepcopy__()函数-深拷贝

    2024-03-22 01:38:01       63 阅读
  3. C++ 拷贝构造函数

    2024-03-22 01:38:01       56 阅读
  4. C++ 拷贝构造函数

    2024-03-22 01:38:01       54 阅读
  5. [C++] 拷贝构造函数 && 深拷贝、浅拷贝

    2024-03-22 01:38:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 01:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 01:38:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 01:38:01       82 阅读
  4. Python语言-面向对象

    2024-03-22 01:38:01       91 阅读

热门阅读

  1. OD C卷 - 员工派遣

    2024-03-22 01:38:01       44 阅读
  2. vue3组合式父页面调用子页面的方法

    2024-03-22 01:38:01       46 阅读
  3. 【Caddy】 Ubuntu 下卸载 Caddy

    2024-03-22 01:38:01       48 阅读
  4. BitMap 和 HyperLogLog

    2024-03-22 01:38:01       42 阅读
  5. LeetCode刷题笔记之hot 100(一)

    2024-03-22 01:38:01       32 阅读
  6. MATLAB中的符号计算是什么?如何使用它?

    2024-03-22 01:38:01       42 阅读
  7. P1170 兔八哥与猎人 Python

    2024-03-22 01:38:01       31 阅读
  8. 蓝桥集训之山峰和山谷

    2024-03-22 01:38:01       43 阅读