本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 拷贝构造函数与拷贝构造函数调用时机
原文链接:https://www.stubbornhuang.com/2412/
发布于:2022年11月16日 11:11:37
修改于:2022年11月16日 11:11:37
1 C++的拷贝构造函数
1.1 拷贝构造函数的含义与形式
C++的拷贝构造函数与构造函数不同,其作用主要是实现从源类对象到目标类对象逐个字节的复制,即目标对象和源对象都等于源对象的值。
C++中类的拷贝构造函数(赋值构造函数)的常用的形式如下
类名(const 类名& exp)
比如我们写一个Example
类,那么它的构造函数、析构函数、拷贝构造函数如下所示
class Example
{
public:
Example()
{
std::cout << "构造函数" << std::endl;
}
virtual~Example()
{
std::cout << "析构函数" << std::endl;
}
Example(const Example& exp)
{
std::cout << "拷贝构造函数" << std::endl;
}
};
那么是什么时候会调用类的拷贝构造函数呢?
1.2 调用类的拷贝构造函数的几种情况
在以下几种情况下会调用类的拷贝构造函数:
- 当使用某类的一个对象初始化该类的另一个对象(引用)时会自动调用拷贝构造函数;
- 当一个函数的形参为一个类的对象时,会自动调用拷贝构造函数;
- 当一个函数的返回值为一个类的对象时,会自动调用拷贝构造函数;
1.2.1 当使用某类的一个对象初始化该类的另一个对象(引用)时
示例代码如下:
#include <iostream>
class Example
{
public:
Example()
{
std::cout << "构造函数" << std::endl;
}
virtual~Example()
{
std::cout << "析构函数" << std::endl;
}
Example(const Example& exp)
{
std::cout << "拷贝构造函数" << std::endl;
}
};
int main()
{
Example example_a;
Example example_b(example_a);
return 0;
}
输出
构造函数
拷贝构造函数
析构函数
析构函数
1.2.2 当一个函数的形参为一个类的对象时
示例代码如下
#include <iostream>
class Example
{
public:
Example()
{
std::cout << "构造函数" << std::endl;
}
virtual~Example()
{
std::cout << "析构函数" << std::endl;
}
Example(const Example& exp)
{
std::cout << "拷贝构造函数" << std::endl;
}
};
void func(Example a)
{
std::cout << "enter void func(Example a)" << std::endl;
}
int main()
{
Example example_a;
func(example_a);
return 0;
}
输出结果
构造函数
拷贝构造函数
enter void func(Example a)
析构函数
析构函数
1.2.3 当一个函数的返回值为一个类的对象时
示例代码如下
#include <iostream>
class Example
{
public:
Example()
{
std::cout << "构造函数" << std::endl;
}
virtual~Example()
{
std::cout << "析构函数" << std::endl;
}
Example(const Example& exp)
{
std::cout << "拷贝构造函数" << std::endl;
}
};
Example copy_return()
{
Example temp;
return temp;
}
int main()
{
Example example_a;
Example example_copy = copy_return();
return 0;
}
输出结果
构造函数
构造函数
拷贝构造函数
析构函数
析构函数
析构函数
当前分类随机文章推荐
- C++ - 数组初始化 阅读367次,点赞0次
- C++ - std::map - 存储动态指针时正确释放内存 阅读4251次,点赞1次
- C++ - Windows/Linux跨平台获取本机CPU核心数 阅读236次,点赞0次
- C++11 - std::bind简要介绍以及可绑定函数的几种形式总结 阅读4691次,点赞4次
- C++ - 将一维数组/二维数组/三维数组作为函数参数传递给函数 阅读1622次,点赞0次
- C++11/std::thread - 线程的基本用法 阅读3310次,点赞0次
- C++ - sleep睡眠函数总结 阅读1107次,点赞0次
- C++11/std::atomic - 原子变量(不加锁实现线程互斥) 阅读6382次,点赞2次
- C++ 11 - final关键字简要介绍 阅读2031次,点赞0次
- C++STL容器 - std::map删除指定元素 阅读2105次,点赞0次
全站随机文章推荐
- NCNN - Windows编译NCNN项目出现无法打开包括文件vulkan.h的错误 阅读169次,点赞0次
- 资源分享 - Geometric and Discrete Path Planning for Interactive Virtual Worlds 英文高清PDF下载 阅读734次,点赞0次
- C++ - C++类的特殊成员函数,析构函数,拷贝构造函数,移动构造函数,赋值运算符,移动赋值运算符介绍和基础语法 阅读880次,点赞0次
- WordPress - 限制非管理员用户进入WordPress后台页面,重定向到首页 阅读1081次,点赞0次
- GCC/GG++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读3921次,点赞3次
- 资源分享 - 实时相机处理技术,Real-Time Cameras中文版PDF下载 阅读1721次,点赞0次
- C++11 - std::bind简要介绍以及可绑定函数的几种形式总结 阅读4691次,点赞4次
- 姿态估计 - COCO-WholeBody数据集骨骼关节keypoint标注对应 阅读3079次,点赞0次
- UnrealEngine4 - Can not find such file SceneRenderTargets.h,在UE4 C++层中正确的使用FSceneRenderTargets类 阅读2789次,点赞0次
- 资源分享 - Geometric tools for computer graphics(Philip J. Schneider, and David H. Eberly)英文高清PDF下载 阅读3467次,点赞0次
评论
169