I'm working on a small project and i'm sort of stuck because I don't really understand how friendship and inheritance interact with each other. I'll show you some sample code.
namespace a
{
class Foo
{
public:
Foo(int x) : m_x(x) {}
protected:
friend class b::Derived;
friend class a::Base;
int m_x;
};
class Base
{
public:
Base(Foo foo) : m_foo(foo) {}
protected:
Foo m_foo;
};
}
namespace b
{
class Derived : public a::Base
{
public:
Derived(a::Foo foo)
: Base(foo)
{
m_foo.m_x;
}
};
}
e0265: at line 29: member a::Foo::m_x (declared at line 10) is inaccessible
Apparently Derived can't access protected members of Foo, seemingly because Derived::m_foo is a derived member, so constructing Derived will fail. Can anyone explain this in detail to me?
I found the problem. The namespace b and therefore the class derived were not visible to the friend declaration in Foo. When I Forward declared b and derived everything worked as intended and derived could access private/protected members.