Why should the copy constructor accept its parameter by reference in C++?(为什么复制构造函数应该在 C++ 中通过引用来接受它的参数?)
问题描述
为什么复制构造函数的参数必须通过引用传递?
Why must a copy constructor's parameter be passed by reference?
推荐答案
因为如果不是按引用,就是按值.为此,您制作一个副本,并为此调用复制构造函数.但要做到这一点,我们需要创建一个新值,所以我们调用复制构造函数,等等......
Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on...
(您将有无限递归,因为要复制,您需要复制".)
(You would have infinite recursion because "to make a copy, you need to make a copy".)
这篇关于为什么复制构造函数应该在 C++ 中通过引用来接受它的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么复制构造函数应该在 C++ 中通过引用来接受


- 将函数的返回值分配给引用 C++? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- GDB 不显示函数名 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01