Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the access Control of C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about how to analyze the access control of C++. The editor thinks it is very practical, so I hope you can learn something after reading this article. Let's take a look at it with the editor.

Before introducing C++ classes, we should first understand what C++ classes are. In fact, the C++ class encapsulates the data member and a series of operations on it. Note: the member function can manipulate the data member, and the friend function includes two kinds: the global function set as the friend. Set to the member function of the friend C++ class.

However, the member function is shared for the whole C++ class, that is, a C++ class retains only one member function. So how does each object relate to these member functions that can be thought of as "separate", that is, how does the member function manipulate the data members of the object?

Remember the this pointer, regardless of whether the object passes through (.) The operation or (- >) operation calls the member function. At compilation time, the compiler will convert this call to our common global function, and add an extra parameter (usually put this parameter in * *), and then pass the this pointer to this parameter. As a result, the binding (or connection) between the object and the member function is completed.

After instantiation, you get several different objects of the same C++ class. Since the member functions are shared, then the member functions can manipulate the data members of the object.

The problem is that there are multiple objects now, and the member function needs to know which object's data member is being operated on?

For example, there are objects obj1 and obj2, both belong to the AC++ class, and the AC++ class has a public member function foo () if obj1 calls this function, the this pointer will be passed to the foo function when compiling, and the members operating obj1 itself in obj1,foo will be accessed directly without any modification, because the data members in the class will be automatically found according to the this pointer.

If obj1 calls this function, you can also access the data members of other objects in the same class as C++! So what you need to do is to let the foo function know which object is the data member of the same C++ class object. One solution is to pass in pointers or references to other objects of the same C++ class, so you can manipulate the data members of other objects of the same C++ class.

Public:protected:private:public inherits publicprotected---protected inherits protectedprotected---private inherits privateprivate

Generally speaking, C++ objects can access each other's data members, but not directly. The step is to call its public member function through an object, which can access its own public/private/protected data members and member functions of other objects of the C++ class (shared by all objects of the C++ class).

You also need to indicate which object's data member (the object calling the function does not need to specify its own members because there is a this pointer; the data members of other objects can be indicated indirectly by references or pointers)

Summary of public,protected,private access in C++ * *: access scope of the private,public,protected method. (inherited by public) private: can only be accessed by functions in this C++ class and its friend functions, cannot be accessed by any other, and objects of this C++ class cannot be accessed.

Protected: it can be accessed by the functions of the C++ class, the functions of the sub-C++ class, and its friend functions, but public cannot be accessed by the object of the C++ class: it can be accessed by the functions of the C++ class, the functions of the sub-C++ class, and its friend functions, or also by the objects of the C++ class. Note: friend functions include two kinds: the global function set as the friend, and the member function of the friend class.

Second, the method property of C++ class changes after inheritance: with private inheritance, all methods of the parent C++ class become private; in the child C++ class, and the protected and public methods of the parent C++ class become the protected,private method in the child C++ class. Using public inheritance, the method properties in the parent C++ class remain unchanged.

The above is how to analyze the access control problems of C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report