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

What are the friends and virtual functions of C++?

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

Share

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

This article mainly introduces the relevant knowledge of what C++ 's friends and virtual functions are, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this C++ 's friend and virtual function article. Let's take a look.

A friend can be a function, which is called a friend function, or a friend can be a class, which is called a friend class. The virtual function must be a non-static member function of the base class, and its access can be protected or public.

Here are a few basics:

1. If class An is a friend of class B, then class A (member function of class B) can directly access the private members of class B.

2. Youyuan cannot be inherited. That is to say, class An is the friend of class B, and class D is a derivative of class B, then class An is not directly the friend of class D. To put it more colloquially, a father's friend is not a born friend of his son.

3. Forget about the basic knowledge of virtual functions.

Let's take a look at the following code:

Code:

Class A; class B {private: virtual void output () {cout Action (); / / Action is public, so you can call}}

Pb calls the non-virtual B::Action function, but calls the virtual function DoAction within the Action, and because pb actually points to the D object, the final call is D::DoAction ()-is this nothing new? As long as you have learned a little bit of C++ polymorphism, you will understand this. Yes, it's so common that almost all C++ programmers write similar code every day-- that's what I'm saying, sometimes it looks like it's calling the code of the base class, but it's actually calling the code of the derived class. Suppose we modify the syntax rules to force virtual functions to fail after meeting friends, that is to force programmers not to use friend, but to write more members who are supposed to be private into public in various ways.

5. Then, there is a concept that seems very simple, but it is misunderstood by many people: did friends break the package? No, Youyuan actually promotes better packaging. It is based on the requirement that there is a class that has so many members (data or functions) that it can only be exposed to individual other classes, and you can consider using friend technology. If not, many people will directly change those members to public, and as a result, properties that should only be open to individual classes become open to all classes. To be tacky, the law stipulates that a wife can look at her husband's butt in private occasions. If the law forbids such an exception, there will probably be some buddies who will just public their buttocks out and take to the streets-if you ask him why, he is also innocent: I just want to make it easier for my wife.

6. After reading the fifth point, C++ programmers who have a set of OO will "test / despise" me. Well, I know that even without the attributes of friend, the aforementioned problem can be satisfactorily realized, similar to the problem of a wife looking at her husband's ass-- I mean, through OO technology, we can avoid the need to open permissions to only part of the class, and transform it into a third party (such as an interface and its implementation class)-- anyway. You have to admit that Youyuan didn't break the encapsulation, because other solutions to this problem seem to be purer OO technology, and their beauty lies in finer particles and better organization (the bad part is that they are a little less efficient, and they have to be a little good for OO, otherwise they will get dizzy, and we won't mention too much in order not to make Cer jokes).

However, even so (point 6), I still have a sophistry: even in those seemingly pure OO languages, there are actually the shadow of friends. For example, Java, which does not have the friend keyword, can have its inner class (non-static inner class) and can access the external class directly, isn't it friendly?-- in fact, this is exactly the use of the main theme of friend on C++ (I don't even have to write "one"). For example, Object Pascal (Delphi) does not have the friend keyword, but as long as it is located in the same unit of code (that is, the same .pas file), all the classes are inherently accessible to each other (of course, visibility needs to be satisfied first)-this is one of the best things I felt when I used Delphi. Since the so-called more OO languages are all good at it, why can't C++?

The seventh point is obviously emotional, which is not in line with what the father of C++ expected of us: "there is a guiding principle in C++ design, that is, no matter what you do, you must trust the programmer." what is much more important than what kind of mistakes can occur is what good things can be done. C++ programmers are always seen as adults. " . In C++ 's world, difference is always respected, and some people don't like to use template, that is; some people insist that as long as private and public are enough, then forget about protected, and some even think that virtual is superfluous-many people just use C++ as another kind of C, that's acceptable. Friend is the same, if you don't use it, its existence will not bring you any performance loss, all you have to do is to use very OO or not OO, but you are familiar with the way to meet the needs of friends.

9. It must be the ninth point. In addition to the friend class, what is more common is the friend function. The overloading of many operators requires a global friend function to reduce the number of public members of the related class. Is it useful to refer to operator overloading at this point? Wow, this is another classic problem. The disputes it has caused are several times more lively than those brought about by this "friendly yuan". Stop right there.

Ten, * I have a suggestion: first have a basic understanding, do some practice, but there is no need to rush to really use it.

The function of friend is to improve the running efficiency of the program, that is, it reduces the time overhead of type checking and security checking, but it destroys the encapsulation and concealment of the class, so that non-member functions can access the private members of the class. The function of virtual function is to realize polymorphism.

This is the end of the article on "what are C++ 's friends and virtual functions". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are C++ 's friends and virtual functions". If you want to learn more knowledge, you are welcome to 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