#include <iostream>
class A {
public:
A(){ cerr << "A Constructor" << endl; }
~A(){ cerr << "A Destructor" << endl; }
A(const A &o){ cerr << "A Copy" << endl; }
A& operator=(const A &o){ cerr << "A Assignment" << endl; return *this; }
};
class B : public A {
public:
B() : A() { cerr << "B Constructor" << endl; }
~B(){ cerr << "B Destructor" << endl; }
private:
B(const B &o) : A() { cerr << "B Copy" << endl; }
B& operator=(const B &o){ cerr << "B Assignment" << endl; return *this; }
};
int main() {
A a;
const A &b = B();
return 0;
}
在GCC 4.2,我得到这个消息:意外const引用行为
In function 'int main()':
Line 16: error: 'B::B(const B&)' is private
compilation terminated due to -Wfatal-errors.
如果我删除了 “私人” 从B,我得到的输出我想到:
A Constructor
A Constructor
B Constructor
B Destructor
A Destructor
A Destructor
我的问题是:为什么要制作一个不被称为private的方法,无论这个代码是否编译?这是标准授权吗?有没有解决方法?
我不明白为什么这不应该编译。 FWIW,Comeau与我同意。 – sbi 2010-07-14 18:22:34
@sbi:奇怪的是,Comeau拒绝禁用C++ 0x扩展的代码,但接受启用了C++ 0x扩展的代码。 – 2010-07-14 18:23:26
g ++ 4.4.2,FWIW没有错误。 – 2010-07-14 18:44:20