Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
José Avatar asked Dec 23 '15 09:12

José


2 Answers

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() {} ).

like image 101
Roman Zaitsev Avatar answered Oct 09 '22 04:10

Roman Zaitsev


It's a compiler bug. It works in VS2008

like image 41
RajeshDA Avatar answered Oct 09 '22 03:10

RajeshDA