struct A
{
A() {}
private:
A(const A&); // Explicitly disable the copy constructor.
};
int main()
{
const A a1; // OK.
A a2; // OK.
auto a3 = const_cast<A&>(a1); // Compiler error C2248! ???
}
我的C++编译器是最新的VC++ 2013预览版。
编译器在最后一行抱怨错误C2248:“A::A”:无法访问在类“A”中声明的私有成员
为什么const_cast不能按预期工作?
const_cast
会生成新的对象? - lulyonconst_cast<A&>
的结果是对已存在对象的引用。声明一个对象会生成一个新对象,而auto a = whatever;
也会这样做(无论whatever
是否是引用)。 - Mike Seymour