这个代码编译和运行没有出错时:为什么我可以从全局范围调用私有构造函数?
class foo{
static foo *ref;
foo(){}
public:
static foo *getRef(){
return ref;
}
void bar(){}
};
foo* foo::ref = new foo; // the construcrtor is private!
int main(int argc, const char *argv[])
{
foo* f = foo::getRef();
f->bar();
return 0;
}
有人能解释为什么能构造被称为?
的确,用'foo * ref = new foo;'(注意不存在'foo ::')来替换表达式来获得编译器错误! –