c++ - 调用对象o=13时无法调用拷贝构造函数;

标签 c++ copy-constructor

<分区>

Object::Object(int x);
Object::~Object;
Object::Object(const &object);

调用时:

Object o=13;

我认为它分为 2 个步骤;

Object temp(13);
Object o=temp;

所以它会调用复制构造函数,但我会打印信息: 没有任何复制构造函数被调用。

所以有人可以帮忙解释一下吗? 我觉得应该和NRVO或者RVO没有关系。

最佳答案

您的示例中省略了复制构造函数。

来自 C++ 标准:

This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

...

— when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

还有一个重要提示:

[ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. —end note ]

关于c++ - 调用对象o=13时无法调用拷贝构造函数;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161138/

相关文章:

c++ - 排序 vector 的最快方法是什么?

c++ - 为什么 C++ 编译器在类具有引用成员时不删除复制构造函数?

c++ - 为什么复制构造函数有时要显式声明为非内联的?

c++ - 如何实现智能容器的拷贝构造函数?

c++ - 打印 GDB 中任何类的 vtbl 函数

c++ - 将数组从 fortran 传递给 C++ 函数

c++ - 如何在 OpenGL(GLUT) 场景中创建静态背景图像?

c++ - OpenCV 调整质量

c++ - 复制构造函数不识别继承的成员

c++ - 使用已删除的函数 std::unique_ptr