假设A类是B类的朋友,B是C类的朋友,A是C的朋友,A是A的朋友,还是两者,或者没有?
解决方法:
不,C中的友谊不是传递性的.
从C 03标准,11.4-8:
Friendship is neither inherited nor transitive. Example:
class A {
friend class B;
int a;
};
class B {
friend class C;
};
class C {
void f(A* p)
{
p->a++;
// error: C is not a friend of A
// despite being a friend of a friend
}
};
class D : public B {
void f(A* p)
{
p->a++;
// error: D is not a friend of A
// despite being derived from a friend
}
};