C++ 我可以显式调用复制构造函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2213894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:34:21  来源:igfitidea点击:

Can I call a copy constructor explicitly?

c++copy-constructor

提问by Alex

I'm a little confused as to the mechanics of the copy constructor. Correct me if I'm wrong:

我对复制构造函数的机制有点困惑。如果我错了纠正我:

If a method takes a reference to an object as a parameter, and the class defines a copy construtor, then the class uses the constructor to create a copy of itself and that gets passed to the function instead of a reference to the original object?

如果一个方法将一个对象的引用作为参数,并且该类定义了一个复制构造函数,那么该类使用构造函数创建自己的副本,并将其传递给函数而不是原始对象的引用?

Furthermore, one can call

此外,可以调用

Object * obj = new Object(&anotherObject);

to create a copy of anotherObject?

创建另一个对象的副本?

回答by

No, if a function take a reference:

不,如果一个函数引用:

void f1( Object & o );   // call by reference

then no copy is made. If a function takes a value:

然后没有复制。如果函数接受一个值:

void f2( Object o );   // call by value

then a copy is created by the compiler using the copy constructor.

然后编译器使用复制构造函数创建一个副本。

And yes, when you say:

是的,当你说:

Object * obj = new Object(anotherObject);   // not &anotherObject

the copy constructor is used explicitly (assuming anotherObject is of type Object.) There is nothing magic about the use of newhere, however - in this case:

显式使用复制构造函数(假设 anotherObject 是 Object 类型。)new然而,这里的使用没有什么神奇之处- 在这种情况下:

Object obj2(anotherObject);

the copy constructor is also used.

还使用了复制构造函数。

回答by Mehrdad Afshari

If a method takes a reference to an object as a parameter, copy constructor will not be called. If that was the case, then a call to the copy constructor itself would have resulted in an infinite loop (since it takes a reference as an argument).

如果方法将对象的引用作为参数,则不会调用复制构造函数。如果是这种情况,则对复制构造函数本身的调用将导致无限循环(因为它将引用作为参数)。

That line is not a valid way to call a copy constructor. It expects a reference as an argument, not a pointer.

该行不是调用复制构造函数的有效方法。它需要一个引用作为参数,而不是一个指针。

回答by AnT

The fact that you are making a method call is of no importance here. Reference parameter initialization during a function call is no different from a standalone reference initialization and is governed by the same rules.

进行方法调用这一事实在这里并不重要。函数调用期间的引用参数初始化与独立的引用初始化没有区别,并受相同规则的约束。

The rules of reference initialization are a bit complicated, but the bottom line is that if the initializer is an lvalue (the argument in the method call in your case) and the type of the reference is the same as the type of the initializer (i.e. type of the parameter is the same as the argument type), then the reference will be bound directly. I.e. no copy is created.

引用初始化的规则有点复杂,但最重要的是,如果初始化器是左值(在您的情况下方法调用中的参数)并且引用的类型与初始化器的类型相同(即参数类型与实参类型相同),则直接绑定引用。即没有创建副本。

Object a; // class type
Object &r = a; // no copying
const Object &cr = a; // no copying

If these requirements are not met (like if the initializer is an rvalue, for example), then it all depends. In some cases the copying might and will take place. For example

如果不满足这些要求(例如,如果初始值设定项是右值),则这一切都取决于。在某些情况下,复制可能并且将会发生。例如

const Object &tr = Object();

can be interpreted by the compiler as

可以被编译器解释为

const Object &tr = Object(Object(Object(Object())));

with implementation-dependent finite number of copyings. Of course, for efficiency reasons compilers normally are trying not to create unnecessary copies, even when they are allowed to copy.

具有依赖于实现的有限数量的复制。当然,出于效率原因,编译器通常会尽量不创建不必要的副本,即使允许复制也是如此。

A classic example that often stirs debate about the validity of the copying behavior of the compiler is the reference initialization in expressions like the following one

一个经常引起关于编译器复制行为有效性争论的经典例子是如下表达式中的引用初始化

Object a;
const Object &r = <some condition> ? a : Object();

A person familiar with C++ reference semantics would understand that expressions like the above are likely the rationale behind the standard permission to perform superfluous copying during reference initialization.

熟悉 C++ 引用语义的人会理解,上述表达式可能是在引用初始化期间执行多余复制的标准权限背后的基本原理。

回答by Naveen

No in both the cases. In the first case, reference to that object itself is passed and copy is not created. In the second case you are passing a pointer to the constructor of objecthence no copy is created. So object should have a constructor (not a copy constructor) which is something like object(anotherClass*)

在这两种情况下都没有。在第一种情况下,传递对该对象本身的引用并且不创建副本。在第二种情况下,您将指针传递给构造函数,object因此不会创建副本。所以对象应该有一个构造函数(不是复制构造函数),就像object(anotherClass*)

回答by zaharpopov

Copy constructor is called only when passing by value, not by reference. By reference no copying is needed (this is part of what references are for!) so no copy constructor called.

复制构造函数仅在按值传递时调用,而不是按引用传递。通过引用不需要复制(这是引用的一部分!)所以没有调用复制构造函数。

回答by HaPpY

yes using placement new like so:

是的,像这样使用新的展示位置:

Object dstObject;
new(&dstObject) Object(&anotherObject);