给出的样本代码:朋友是否看到基类?
class Base {
public:
bool pub;
protected:
bool prot;
};
class Derived : private Base {
friend class MyFriend;
};
class MyFriend {
Derived _derived;
void test() {
// Does standard provide me access to _derived.pub and _derived.prot?
cout << "Am I allowed access to this: " << _derived.pub
<< " and this: " << _derived.prot;
}
};
是否是一个朋友给我的所有访问,就好像我是在类中的成员函数我会得到到我的朋友吗?换句话说,我可以得到自从我是朋友以来私下继承的基类的受保护和公共成员吗?
看到,因为你已经去麻烦写上去的示例代码,你试过*编译它*?这种答案会很快暴露在警告/缺乏之中。 – ssube 2012-03-26 19:51:42
@peachykeen:编译器接受什么,标准说什么通常是不同的东西。另外,从理论上讲,示例代码无法捕捉到的细微之处。 – 2012-03-26 20:13:54
@AdrianMcCarthy的确如此。但是,许多编译器会在使用非标准功能时发出警告,如果违反标准和编译器的实现,您将得到一个简短而甜蜜的答案。虽然不是万无一失,但尝试并不会伤害。 – ssube 2012-03-27 13:43:06