As the name suggests, private/public/protected respectively indicates "private/public/protected", which is a group usedAccess permissionControl keywords.
So,WHO (visitors) should be controlled to access (who are being visited) permissions?
"WHO (visited )"It is clear that a member of a class (including member variables and member methods ). However,"Who (visitor )"But vague. In fact, it refers toFunction, not class(Not a variable ).
Private/public/protected:Function (visitor)ForClass member (including member variables and member methods). Therefore:
1)Youyuan (including all member functions of youyuan function or youyuan class)You can access anyMember(Including member variables and member methods );
2) In addition to youyuan, privateMember (including member variables and member methods)OnlyMember functions of this classAccessible; protectedMember (including member variables and member methods)Only the member functions of this class and Their Derived classes can be accessed; PublicMember (including member variables and member methods)Can be accessed by all functions.
That is to say, when we say that a class can access XXX, it actually impliesMember functions of this classYou can access XXX.
Supplement:There is a technology called member spy (class member spyware). Through this technology, the derived class can modify the protected member of the base class to the public permission. This technology uses the using keyword. Example:
Class A <br/>{< br/> protected: <br/> int m_data; <br/>}; </P> <p> class spya: public A <br/>{< br/> Public: <br/> using a: m_data; <br/> }; </P> <p> void testspy (A * pA) <br/> {<br/> spya * pspya = static_cast <spya *> (PA ); <br/> // forcibly convert a to spya. This requires that spya has no member variables and does not overload the virtual functions in. <Br/> // now you can access m_data through pspya. Example: int DATA = pspya-> m_data; <br/>}
Because this technology uses forced type conversion, use it with caution.