Here is my codes in file source.cpp:
class B
{
friend class F;
protected:
int protectedIntB;
};
class D : public B {};
class F
{
public:
int f(D &d) {return ++d.protectedIntB;}
};
When I compile above codes with g++ -c -Wall -pedantic -std=c++11 source.cpp and cl /c source.cpp, both compilers compile successfully. However, when I make D inherits from B using protected instead of public:
class D : protected B {};
This time, gcc compiles successfully while cl gives an error says that B::protectedIntB is inaccessible in return ++d.protectedIntB;.
Another situation is replacing public with private:
class D : private B {};
This time, both compilers yield errors. By the way I'm using gcc version 5.3.0 built by mingw-w64 and cl version 19.00.24210 from VS2015.
Here comes my question:
How does friend class of the base class access members of that base class through objects of class derived from the base class, and why gcc and cl handle it differently?
Edit:
Thanks to songyuanyao and Brian, it seems a bug in gcc 5.3.0 in the protected case. Only the public case should be compiled successfully, and gcc 6.1.0 also works fine.