gpt4 book ai didi

c++ - 为什么传递临时对象时不调用复制构造函数

转载 作者:行者123 更新时间:2023-11-28 02:08:11 24 4
gpt4 key购买 nike

class MyClass
{
public:
int a;
MyClass(int r): a(r) {}

MyClass(const MyClass& ref)
{
cout << "Copy Constructor\n";
a= ref.a;
}
};
int main()
{
MyClass obj(5);
MyClass obj1(MyClass(5)); //Case 1
MyClass obj2(obj); //Case 2
return 0;
}

为什么复制构造函数在情况 2 中被调用,而不是在情况 1 中。在情况 1 中,临时对象作为参数传递。

最佳答案

MyClass obj1(MyClass(5)); 中,编译器省略了临时对象 MyClass(5),因为这是允许的。

特别是,C++ 标准 2014 §12.8 第 31 段定义了可以执行复制省略的情况:

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects... 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.

关于c++ - 为什么传递临时对象时不调用复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com