C + + access control character detailed __c++

Source: Internet
Author: User
Tags access properties control characters object model
First, the preface

The C + + standard defines three kinds of member access control characters: public, protected, and private, respectively, and individually, protected, and privately owned. Of course, this is the idea of declaring these three kinds of controls inside a class, and what would it be like in an inheritance system.


second, the control character access characteristics

First, the access control characteristics of these three access control characters are described, indicating which entities the variables declared by these three control characters can be accessed by:

(1) Public member: can be the member function of this class (either the member function declared by the controller), the friend function, the object of this class, the member function of its derived class (regardless of which is the member function declared by the control character);

(2) Protected member: can be the member function of this class (either the member function declared by the controller), the friend function, the member function of its derived class (regardless of which is the member function declared by the control character);

(3) Private member: can be the member function of this class (regardless of which kind of control character declares member function, all can), friend function;

Through the above introduction, can be summed up as:

(1) For the users of the class , that is, the person who uses the class object instance (there is also a term for people who see only the header file of a class, because the design class has compiled the implementation file that he wrote into a binary file, which is a file of the. lib,. obj suffix under windows), You can access only public members (functions or variables) of a class through a class object instance;

(2) for the class itself , the designer of the class, in the process of designing the class, he can use all the member functions of the class (three control characters declare), or the friend function accesses all the members (functions or variables) within its class, regardless of which control character the member (function or variable) is declared with. can be accessed.

(3) for a derived class of this class, it is only possible to say that the members of the base class public and protected are visible to it, that is, accessible, but specifically how to access it, which involves the knowledge of the C + + class inheritance, followed by an emphasis on this article.


Third, C + + access control and inheritance

In an inheritance system, there are two levels of access control characters, one layer in the derived list of the class, and the other in the base class.  the huge and complex inheritance system seems to be hard to understand (it's really hard to understand, haha), but it boils down to a single layer of inheritance, so here I use the single layer public inheritance structure to illustrate the relationship between C + + access control and inheritance, the inheritance structure is drived : Public Base.

Since this involves inheritance, the first thing to understand is the object model of the derived class in the C + + inheritance system, which is explained in C++primer and the Deep Exploration C + + object model, that is, the memory model of the derived class consists of two parts: the base class part and the derived class part. Where the base class is inherited from the base class, the derived class part is defined by itself.

The first-level access control character (that is, the derived list) affects the access control permissions (and only that effect) of some members of the base class (or a function or variable) in the derived class, and the specific effect is:

(1) Public inheritance ( remain unchanged): The access control permission of a member (function or variable) of a base class part in a derived class is the same as the permission declared in the base class;

(2) Protected inheritance ( each of which lowers one permission ): The public member in the base class becomes protected in the derived class, and the other member becomes the private permission;

(3) Private inheritance ( all privatization ): The access control permissions for the members (functions or variables) of the base class part of the derived class are all changed to private members.

Well, now that the first level of access control is explained, all members of the derived class (the base class is partly explained by the above, and the derived class part itself is declared) has the access Control permission , and the question now is whether it can be accessed.

For the example of a single-layer public inheritance structure here, the access control permissions of the members of the base class section remain the same, as explained in three different cases:

(1) A public member of the base class section. Note that although the memory model of the derived class is divided into two parts, both parts belong to the derived class object . All member functions of the derived class (the class itself) , regardless of which access control character, can access the public member (function or variable) of the base class part, and the derived class object can also be accessed directly (through the member access operator).

(2) Protected members of the base class section. All member functions of the derived class (the class itself) (regardless of which access control character) can access the protected member (function or variable) of the base class part, but the derived class object cannot Direct access (the base class object itself cannot access its protected members, of course, derived classes are not possible).

(3) Private members of the base class section. All member functions of the derived class (the class itself) (regardless of which access control character) cannot access private members (functions or variables) of the base class part, nor are the derived class objects directly accessible .

The following three kinds of cases in the VS2010 test, here only for editing, VS2010 automatic error detection function can prompt the corresponding errors

Class Base
{public
:
	int pub_mem;
Protected:
	int pro_mem;
Private:
	int pri_mem;

For the class itself, its member functions, regardless of the permission descriptions, can access any of the permissions in the class. Member variable public
:
	int base_fun_pub () {}
protected:
	int Base_fun _pro () {}
private:
	int base_fun_pri () {}
};

Class Drived:public base
{
//pub, Pro, PRI member functions can access public, protected member variables of its base class section, but none can access private member public
:
	int drived_fun_pub ()
	{
		pub_mem = 0;     Situation (1)
		pro_mem = 0;     Situation (2)
		pri_mem = 0;     Situation (3) (Error, inaccessible)
	}
protected:
	int Drived_fun_pro ()
	{
		pub_mem = 0;    Situation (1)
		pro_mem = 0;    Situation (2)
		pri_mem = 0;    Situation (3) (Error, inaccessible)
	}
private:
	int drived_fun_pri ()
	{
		pub_mem = 0;   Situation (1)
		pro_mem = 0;   Situation (2)
		pri_mem = 0;    Situation (3) (Error, inaccessible)
	}
private:
	int J;
};

void Main ()
{
	/****************** single layer public inheritance as an example **********************/
	drived drivedobj;
	Drivedobj.pub_mem;    Situation (1)
	Drivedobj.pro_mem;    Situation (2) (Error, inaccessible)
	Drivedobj.pri_mem;    Situation (3) (Error, inaccessible)
}
The result that is displayed in VS2010 is (the automatic red line prompts is the access right error):


Iv. Summary

In fact, regardless of inheritance or inheritance, you can consider an Access Control permission analysis in a single layer class (No inheritance), because there is nothing more than an access control character in an inherited list. This control character affects the access rights of the members in the base class in the derived class (only this affects), and this effect is very good to explain (see sect. III above), once the access control character in the derived list is interpreted, it can be viewed as a member of a single class, so The steps for interpreting member access permissions for a class are:

1, if it is a derived class, remember two points

(1) first explain the control characters in the derived list, and determine what properties the members of the base class are in the derived class;

(2) for derived classes, a derived class object cannot access any member of the base class, and the derived class itself (a member function) can access only the members of the base class that have the original property of protected and public (note that the original property, regardless of the property after the derivation list access control is interpreted).

2, if not a derived class

(1) Its object can access only members of the public property;

(2) for the interface implementation Code of a class (the member function of all three attributes), it can access all members of the access rights.

So, for the inheritance system, there is no doubt that there is a further layer of doubt: that is, the members of the base class in the derived class is what access properties. Once this problem is solved, it becomes a single class access problem.


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.