?????? C++ ????????????
??????????????С????????????????????????е?????????
???????????????????
????+---------+
????|class    | --> ??
????+---------+
????|public   | --> ??????
????|protected| --> ???????
????|private  | --> ??????
????+---------+
????|friend   | --> ??????
????+---------+
???????????????????????????????????????????????????????????Щ?????????????????????????????????????????鴫?У???????????????????????????????????????????????????????????????????????Щ??????????????????????????????м?λ?????????????
???????????????????
????+---------+
????|Inherits | --> ??????
????+---------+
????|public   | --> ???
????|protected| --> ????
????|private  | --> ???
????+---------+
????C++ ???????м????????????????????????????????????????????????????????????public??????????????????public?? protected??????????????protected????????????????????????????????protected?? protected?????????????????——??????private???????????????????????????????????private??private????
class Father {
public:
int x;
protected:
int y;
private:
int z;
};
class FirstSon : public Father {
// x is public
// y is protected
// z is not accessible from Father
};
class SecondSon : protected Father {
// x is protected
// y is protected
// z is not accessible from Father
};
class Illegitimate : private A {
// x is private
// y is private
// z is not accessible from Father
};
?????????????????????????????????????
class Base {
protected:
int prot_mem;
};
class Sneaky : public Base {
friend void clobber(Sneaky &s) { s.j = s.prot_mem = 0; }
friend void clobber(Base &b) { b.prot_mem = 0; } // error
int j;
};
????prot_mem ?? Base ?? protected ???????????? ?? public ???????? Base ?????? Sneaky ??????????????ó????????????
??????????? clobber ??? Sneaky ?????????????????? Sneaky ??????????????? j?????????????? prot_mem??
????????? clobber ??? Sneaky ???????????????????丸?????μ? prot_mem???????????????? ????????????????????????????????? Sneaky?????????丸???????????Υ???????????