#include#include #include using namespace std; struct B; extern void F(B* b); struct B { struct A { friend void F(); }; private: int data; }; void F(B* b) { cout << b->data << endl; } int main() { B b; F(&b); return 0; }
编译无法通过,因为A只是赋予了F访问A所有成员的能力。尽管A是B的内部类,可以看到B的所有东西,但是A不能替B做出friend的这个决定,而访问B中的成员。
class_friend.cpp: In function ‘void F(B*)’:
class_friend.cpp:36:15: error: ‘int B::data’ is private within this context
cout << b->data << endl;
^~~~
class_friend.cpp:30:8: note: declared private here
int data;



