C + + Member permissions control (summary)

Source: Internet
Author: User
Tags access properties visibility

1) Preface

In my study of C + +, the control of the members of the class has always been a point of headache, a public, a will and private, there are protected, and then add inheritance, but also has the common inheritance, private inheritance, protection of inheritance, so the feeling will be more chaotic.

Later in the process of continuous learning, slowly straighten out their relationship, a little classification summed up, in this issue is basically no longer tripped. If there is a clerical error, I hope the great God pointing!

2) C + + level permission control

First explain a few specific words, the following are used:

Horizontal permissions: In a class, the permission control of a member is the ability of a member function in a class to access another member, the object of the class can access a member of the class.

Vertical permission: In a derived class, access to a member inherited from a base class.

Internal access: A member function in a class accesses other members.

External access: Through the object of the class, access to the class's member functions or member variables, and some books are also called Object access.

Here the "level" is for easy to understand the use of, in fact, can also use "horizontal" such words, other words similar, the term meaning is not big, but to deepen understanding can be more effective!

when private,public,protected purely as a member (variable and function) in a class when the permission is set:

A member function of a class and a friend function can access all members of a class, but outside the class through the class's object, you can access only the members of that class.

Note: The friend function consists of two kinds: global function set as friend, set as member function in friend class; here the friend function is considered as internal function, convenient to remember!

This is summarized in the following table:

Properties in class

Private

Protected

Public

Internal visibility

Visible

Visible

Visible

External visibility

Not visible

Not visible

Visible

The program verifies the following: There are no friend members here, and the member variable and the member function have the same permission control.

1#include <iostream>2 3 classFoo4 {5  Public:6     intA;7     voidPublic_fun ();8 protected:9     Charb;Ten     voidProtected_fun (); One Private: A     BOOLC; -     voidPrivate_fun (); - }; the  - //To verify the internal visibility of public members - voidFoo::p ublic_fun () - { +A =Ten; -b ='C'; +c =true; A } at  - //verifying the internal visibility of the protected member function - voidFoo::p rotected_fun () - { -A =Ten; -b ='C'; inc =true; - } to  + //to verify the internal visibility of the private member function - voidFoo::p rivate_fun () the { *A =Ten; $b ='C';Panax Notoginsengc =true; - } the  + intMain () A {   the foo foo; +Foo.public_fun ();//To verify the external visibility of public members -Foo.protected_fun ();//verify the external visibility of public members, where errors are indicated $Foo.private_fun ();//verify the external visibility of the private member, where the error is indicated $     return 0;  -}
3)Vertical access Control for C + + when private,public,protected as a way of inheriting:

Derived classes can inherit members that are outside the base class except for constructors and destructors, which are not inherited by the class of the specific object, and cannot be inherited by an assignment operator overload function, but the access properties of those members can be adjusted in the derivation process. The access properties of a member inherited from a base class in a derived class are controlled by inheritance.

Properties in base class

Private

Protected

Public

Private

Protected

Public

Private

Protected

Public

How to Inherit

Private

Protected

Public

Internal visibility

Not visible

Private

Private

Not visible

Protected

Protected

Not visible

Protected

Public

External visibility

Not visible

Not visible

Not visible

Not visible

Not visible

Not visible

Not visible

Not visible

Visible

Derived classes have the following two types of access to base class Members:

(1) Internal access: Access to members inherited from the base class by the new member function in the derived class.

(2) External access: Outside of a derived class, access to a member inherited from a base class by an object of the derived class.

4) Understanding of authority control

C + + permissions control, give people feel a bit cumbersome, especially three kinds of permissions, plus three kinds of inheritance, there are friends of those things, very let the head big, here to summarize, basically the most common and commonly used to clarify, some of this problem, such as the actual work or written questions, can be solved.

C + + Member permissions control (summary)

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.