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 use C++ friends

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article introduces "how to use C++ friends" in detail. The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use C++ friends" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply. Let's learn new knowledge.

I. the concept of friends

What is Youyuan?

Friend is a kind of relationship in C++.

Friend relations occur between functions and classes or between classes.

Friendship is a single item and cannot be passed on.

Second, the usage of friends

Declare friends in a class with the friend keyword

The friend of a class can be another class or a concrete function

Friends are not part of the class.

Friends are not restricted by the access level in the class.

Friends can directly access all members of a specific class

Third, the grammar of Youyuan

Declare a function or class in a class with the friend keyword

First, take a look at a code that does not use friends:

# include # include class Point {double x; double y: Point (double x, double y) {this- > x = x; this- > y = y;} double getX () {return x;} double getY () {return y;} / friend double func (Point& p1, Point& p2);}; double func (Point& p1, Point& p2) {double ret = 0 Ret = (p2.getY ()-p1.getY ()) * (p2.getY ()-p1.getY ()) + (p2.getX ()-p1.getX ()) * (p2.getX ()-p1.getX ()); ret = sqrt (ret); return ret;} int main () {Point p1 (1,2); Point p2 (10,20) Printf ("p1 (% f,% f)\ n", p1.getX (), p1.getY ()); printf ("p2 (% f,% f)\ n", p2.getX (), p2.getY ()); printf ("| (p1, p2) | =% f\ n", func (p1, p2)); return 0;}

The output is as follows:

This program needs frequent access to private members x and y when calculating the distance between two points in x and y, so it has to call getX () and getY () to access x and y. Eight calls to getX () and getY () in the x and y functions are troublesome.

At this time, it's time for our friends to play:

# include # include class Point {double x; double y: Point (double x, double y) {this- > x = x; this- > y = y;} double getX () {return x;} double getY () {return y;} friend double func (Point& p1, Point& p2);}; double func (Point& p1, Point& p2) {double ret = 0 Ret = (p2.y-p1.y) * (p2.y-p1.y) + (p2.x-p1.x) * (p2.x-p1.x); ret = sqrt (ret); return ret;} int main () {Point p1 (1,2); Point p2 (10,20); printf ("p1 (% f,% f)\ n", p1.getX (), p1.getY ()) Printf ("p2 (% f,% f)\ n", p2.getX (), p2.getY (); printf ("| (p1, p2) | =% f\ n", func (p1, p2)); return 0;}

The output is as follows:

Fourth, the embarrassment of Youyuan

Friends are born to take into account the efficiency of the C language.

Friends directly destroy the encapsulation of object-oriented.

The efficiency of Youyuan in the actual product outweighs the gain.

Youyuan has been gradually abandoned in modern software engineering.

V. matters needing attention

Friendly relations are not transitive.

The friend of a class can be a member function of another class.

The friend of a class can be a complete class

All member functions are friends

Let's take an in-depth analysis of Youyuan:

# include class ClassC {const char* n: ClassC (const char* n) {this- > n = n;} friend class ClassB;}; class ClassB {const char* n: ClassB (const char* n) {this- > n = n;} void getClassCName (ClassC& c) {printf ("c.n =% s\ n", c.n);} friend class ClassA;} Class ClassA {const char* n: ClassA (const char* n) {this- > n = n;} void getClassBName (ClassB& b) {printf ("b.n =% s\ n", b.n);} / * void getClassCName (ClassC& c) {printf ("c.n =% s\ n", c.n);} * /} Int main () {ClassA A ("A"); ClassB B ("B"); ClassC C ("C"); A.getClassBName (B); B.getClassCName (C); return 0;}

B is the friend of C and An is the friend of B. the output is as follows:

Since A can access BMIT B can access C, can An access C? Uncomment the above code:

Void getClassCName (ClassC& c) {printf ("c.n =% s\ n", c.n);}

The output reports an error, which indicates that the friendly relationship is not transitive.

After reading this, the article "how to use C++ friends" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about the article, welcome to 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