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++ 中通过引用接受其参数?


- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01