What is a copy constructor in C++?(什么是 C++ 中的复制构造函数?)
问题描述
在 Scott Meyers 的 Effective C++ 的第 6 页中,定义了术语复制构造函数".我一直在使用 Schiltdt 的书作为我的参考,但我找不到任何提到复制构造函数的内容.我明白了,但这是 C++ 的标准部分吗?当按值传递类时,这样的构造函数会被调用吗?
On page 6 of Scott Meyers's Effective C++, the term 'copy constructor' is defined. I've been using Schiltdt's book as my reference and I can find no mention of copy constructors. I get the idea but is this a standard part of c++? Will such constructors get called when a pass a class by value?
推荐答案
复制构造函数具有以下形式:
A copy constructor has the following form:
class example
{
example(const example&)
{
// this is the copy constructor
}
}
以下示例显示了它的调用位置.
The following example shows where it is called.
void foo(example x);
int main(void)
{
example x1; //normal ctor
example x2 = x1; // copy ctor
example x3(x2); // copy ctor
foo(x1); // calls the copy ctor to copy the argument for foo
}
这篇关于什么是 C++ 中的复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是 C++ 中的复制构造函数?


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