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 friend function friend in C++

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

Share

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

In this issue, the editor will bring you about how to analyze the friend function friend in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

We know that C++ controls access to private parts of objects only through public interfaces. Of course, this kind of design is right, but sometimes it will appear too strict and cause some problems.

Therefore, C++ provides another form of access, called friend.

There are three kinds of friends, which are friend function, friend class and friend member function.

By making the function a friend of the class, we can give the function the same access as the class member function, that is, we can access the private member variables of the class in the friend function.

Before we introduce the use of friend functions, we need to understand why friend functions are needed. A very good example is given in C++ Primer. In the previous example of operator overloading, we implemented a class Time. To record time, suppose we need to overload its * operator to allow a time object to be multiplied by a floating-point number.

Obviously, we only need to overload the operator *:

Time Time::operator* (const double x) {/ / todo}

When we use it, it looks like this:

Time a, bttera = b * 32.5

But there is a small problem. We write it as a = b * 32.5; yes, but not if it is written as 32.5 * b the other way around. Because for b * 32.5, it is essentially b that calls the operator* function, which is equivalent to a = B. opeartor * (32.5);. But the latter is not good, how to solve it, can only be solved by implementing another function, this function has two input, which is double and Time type, and returns a Time type.

Time operator* (double m, const Time & t)

However, there is a new problem, because this is not a member function, you cannot directly access the private data of the class. In order to make it accessible as an exception, we need to set it as a friend.

The way to create friends is simple, we just need to add the keyword friend before the function signature.

Friend Time operator* (double m, const Time & t)

It has two meanings:

It is not a member function, so you cannot use the member function operator to call the

It has the same access rights as member functions, that is, it can access all private and public data

Since the friend function is not a member function, we do not need to use the Time:: qualifier or add the keyword friend to the implementation.

The implementation of the function is as follows:

Time operator* (double m, const Time & t) {Time result; long totalminutes = t.hours * m * 60 + t.minutes * m; result.hours = totalminutes / 60; result.minutes = totalminutes% 60; return result;}

We have direct access to the hours and minutes member variables in the function, so the function must be a friend function.

Of course, we can change the function a little so that it doesn't have to be a friend function:

Time operator* (double m, const Time & t) {return t * m; / / t.operator* (m)}

In this function, we don't explicitly access private variables, so we don't have to be friends.

The above is the editor for you to share the C++ friend function friend how to analyze, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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