简体   繁体   中英

friend class cannot access protected member

This code used to work fine with Visual Studio 2015, but it not longer works with Visual Studio 2015 update 1.

class Foo
{
protected:

    virtual ~Foo() {};
    friend class Foo__init;
};

class Foo__init
{
public:

    Foo _init;
};

static Foo__init _Foo_init;

It fails with the following error:

Error   C2248   'Foo::~Foo': cannot access protected member declared in class 'Foo'

Is this a compiler bug or is the code ill formed?

From 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.

So it's a compiler bug. Both g++ (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010 and Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on LLVM 3.6.2) compiles this code ( I added int main() {} ).

It's a compiler bug. It works in VS2008

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM