I've come over a little discussion today, whether it's necessary to explicitly declare friend
access for an inner class
/struct
. Here's the (replicating sample) code in question:
struct Interface
{
virtual void foo() = 0;
virtual ~Interface() {}
};
class Implementation
{
struct InterfaceImpl : Interface
{
InterfaceImpl(Implementation* impl)
: impl_(impl) {}
virtual void foo()
{
impl_->doFoo(); // << Here's what's in question!!
}
Implementation* impl_;
};
public:
Implementation()
: interfaceImpl_(this) {}
Interface* getInterface() { return &interfaceImpl_; }
private:
InterfaceImpl interfaceImpl_;
void doFoo() {}
};
int main() {
Implementation impl;
return 0;
}
I've been noticing that the code compiles well, where I thought it would be necessary to have a friend struct InterfaceImpl;
at the Implementation
class to get it working. So the following setups all work fine: c++11, GCC 4.8.1, GCC 4.3.2.
Is there a c++ (pre-c++11) standard section that confirms this is legal?