【发布时间】:2013-12-25 15:54:16
【问题描述】:
拥有一个公共复制构造函数将使这个小程序 编译,但不显示副作用“复制”。
#include <iostream>
class X
{
public:
X(int) { std::cout << "Construct" << std::endl; }
// Having a public copy constructor will make the little program
// compile, but not showing the side effect "Copy".
private:
X(const X&) { std::cout << "Copy" << std::endl; }
private:
X& operator = (const X&);
};
int main() {
X x = 1;
return 0;
}
【问题讨论】:
-
X x=1 表示 X x(X(1)) 据我所知,但它已优化为 X x(1);
-
它是必需的,以便 C++ 代码在实现之间可移植,这些实现可能会或可能不会自行决定执行复制省略。
-
尝试使用
-fno-elide-constructors标志进行编译。
标签: c++ oop constructor