这是说明问题的最小代码示例:
#include <iostream>
class Thing
{
// Non-copyable
Thing(const Thing&);
Thing& operator=(const Thing&);
int n_;
public:
Thing(int n) : n_(n) {}
int getValue() const { return n_;}
};
void show(const Thing& t)
{
std::cout << t.getValue() << std::endl;
}
int main()
{
show(3);
}
这会产生相同的错误:
int main()
{
show( Thing(3) );
}
AIX 下的 IBM XL C/C++ 8.0 编译器发出以下警告:
"testWarning.cpp", line 24.9: 1540-0306 (W) The "private" copy constructor "Thing(const Thing &)" cannot be accessed.
"testWarning.cpp", line 24.9: 1540-0308 (I) The semantics specify that a temporary object must be constructed.
"testWarning.cpp", line 24.9: 1540-0309 (I) The temporary is not constructed, but the copy constructor must be accessible.
我还使用“-Wall”和“-pedantic”尝试了 g++ 4.1.2,但没有得到诊断。为什么这里需要访问复制构造函数?除了使对象可复制(不在我的控制范围内)或使显式副本通过(当现实生活中的对象复制成本很高时)之外,我如何消除警告?