Keyword of access control for C ++: private/public/protected

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.