什么是 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
}
相关文章