判断一下下面这段代码能否通过编译?
#include <iostream>
#include <string>
#include <memory>
using namespace std;
struct B;
extern void F(B* b);
struct B
{
struct A
{
friend void F(B* b);
};
private:
int data;
};
void F(B* b)
{
cout << b->data << endl;
}
int main()
{
B b;
F(&b);
return 0;
}
<label class="jsk-checkbox"> 不能。
</label>因为A
只是赋予了F
访问A
所有成员的能力。尽管A
是B
的内部类,可以看到B
的所有东西,但是A
不能替B
做出friend
的这个决定。