C++ 入門第 5 版から:
これらのクラスを見てください:
class Base {
friend class Pal;
public:
void pub_mem();
protected:
int prot_mem;
private:
int priv_mem;
};
class Sneaky : public Base {
private:
int j;
};
class Pal {
public:
int f1(Base b){
return b.prot_mem; //OK, Pal is friend class
}
int f2(Sneaky s){
return s.j; //error: Pal not friend, j is private
}
int f3(Sneaky s){
return s.prot_mem; //ok Pal is friend
}
}
ここで、Pal は Base のフレンド クラスですが、Sneaky は Base から継承され、Pal で使用されます。が呼び出される最後の行でs.prot_mem
、作成者はそれが機能する理由を示しています。なぜなら、Pal は Base のフレンド クラスだからです。しかし、私がこれまで読んだことは、私の理解では、s は Base から派生し、prot_mem は既に Sneaky にアクセスできるはずの Base Class の保護されたメンバーであるため、とにかく動作するはずであることがわかります。お願いします?