此代码曾用于Visual Studio 2015,但它不再适用于Visual Studio 2015更新1.
class Foo { protected: virtual ~Foo() {}; friend class Foo__init; }; class Foo__init { public: Foo _init; }; static Foo__init _Foo_init;
它失败并出现以下错误:
Error C2248 'Foo::~Foo': cannot access protected member declared in class 'Foo'
解决方法
从
here开始:
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
所以这是一个编译器错误. g(Ubuntu 5.2.1-22ubuntu2)5.2.1 20151010和Ubuntu clang版本3.6.2-1(标签/ RELEASE_362 / final)(基于LLVM 3.6.2)编译此代码(我添加了int main(){} ).