C ++ FAQ Lite [14] - Friends (Update)

zhaozj2021-02-08  264

[14] Friends (Part of C FAQ Lite, Copyright © 1991-2001, Marshall Cline, Cline@parashift.com)

体,, nicrosoft @ sunistudio.com (East day production room, East day document)

FAQS in section [14]:

[14.1] What is Friend? [14.2] Do you destroy the package? [14.3] What is the advantages and disadvantages of using a friend function? [14.4] "Friends Relationships are neither inherited, nor passing" what does it mean? [14.5] What should I use member functions or a friend function?

[14.1] What is Friend?

Allow another class or function to access your class. Friends can be functions or other classes. Category given special access to it. Usually the same developer will control the friend and member functions of the technology and non-technical (otherwise when you want to update your class, you have to get the consent of the owner of other parts).

[TOP | BOTTOM | Previous Section | Next Section]

[14.2] Does the friend have destroyed the package?

[Recently Made A Bit More Emphatic (on 4/01). Click Here to Go To The Next Faq in The "Chain" of Recent Changes

]

If properly used, you can actually enhance the package. When a class of two parts have different quantities or different life cycles, you often need to split a class into two parts. In these cases, both parts typically need to directly access each other's data (these two parts are originally in the same class, so you don't have to increase the code directly to access a data structure; you only need to change the code to two classes. ). The safest way to achieve this is to make these two parts become friends with each other. If you use friendly as you just described, you can keep private (private). People who don't understand these people really want to avoid using friends, they either use public data (rare!), Either through public GET () and set () member functions to make two parts You can access the data. And they actually destroy the package. The GET () and set () member functions for private data settings are reasonable only when they are "exhibited by the private data). In many cases, these GET () / set () member functions are as bad as public data: they only hide the name of private data, and there is no hidden private data itself. Similarly, if you use the friend function as a class of public: The syntax of the access function is used, the friend function will destroy the encapsulation with the members of the package. For a statement, the class friend does not destroy the packaged barriers: Like the members of the class, they are the barriers of the package.

[TOP | BOTTOM | Previous Section | Next Section]

[14.3] What is the advantages and disadvantages of using a friend function?

The friend function provides a certain degree of freedom in interface design choice.

Member function and friend function have the same privilege (100%). The main difference is that the friend function is called, and the member function is called X.f (). Therefore, the capabilities you can select between the member function (X.f ()) and friends (F (x)) allow the designer to choose what he thinks more readable is to reduce maintenance costs. The main disadvantage of the friend function is that the additional code is required to support dynamic binding. To get the effect of Virtual Friend, the friend function should call a hidden (usually protected :) virtual member function. This is called the Virtual Friend Function Idiom. For example: Class base {public: Friend Void F (Base & B);

// ...

Protected: Virtual Void DO_F ();

// ...

}; Inline void f (base & b) {b.do_f ();} Class Derived: Public Base {public:

// ...

Protected: Virtual Void DO_F ();

// "Overlay" F (Base & B) behavior

// ...

}; Void Usercode (base & b) {f (b);}

The F (b) statement in usercode (base "will call the virtual B.DO_F (). This means that if b is actually a derived object, then Derived :: DO_F () will get control. Note that the derived class is covered by the protected virtual () member function DO_F (); instead of its friend function F (Base &).

[TOP | BOTTOM | Previous Section | Next Section]

[14.4] "Friends Relationships are neither inherited, nor passing" what does it mean?

[Recently Added The "Not Reciprocal" Item THANKS to Karel Roose (on 4/01). Click Here to Go To The Next Faq in The "Chain" of Recent Changes

]

Just because I admit your friendship to you, allow you to access me, do not automatically allow your child to access me, do not automatically allow your friends to access me, do not allow me to access you.

I don't have to trust my friend's children. The privilege of the friend is not inherited. Friends' derived class is not necessarily a friend. If the Fred class declares that the base class is a friend, the Base class's derived class does not automatically confer access privileges for Fred objects. I don't have to trust my friend's friend. The privilege of the friend is not passed. Friends' friends are not necessarily a friend. If the Fred class declares that the Wilma class is a friend, and the Wilma class declares the Betty class is a friend, then the Betty class does not automatically confer access privileges for Fred objects. You don't have to just because I claim to be my friend, I trust me. Friends' privileges are not overlap. If the Fred class declares that the Wilma class is a friend, the Wilma object has privileges to access the Fred object, but the Fred object does not automatically have access to Wilma objects.

[TOP | BOTTOM | Previous Section | Next Section]

[14.5] What should I use member functions or a friend function?

Try to use the member function, you must use the friend when you do it. Sometimes in grammar, friends better (for example, in the Fred class, the friend function allows the Fred parameter as the second parameter, and the member function must be the first). Another good usage is a binary infix operator. For example, if you want Afloat Acomplex, Acomplex Acomplex should be defined as a friend instead of a member function. (The member function does not allow lift the left parameters, because it will change the class of the member function to call the object). In other cases, the preferred member function. [TOP | BOTTOM | Previous Section | Next Section]

E-mail the author [C FAQ Lite | Table of Contents | Subject Index | About The Author | © | Download Your Own Copy] Revised Apr 8, 2001

转载请注明原文地址:https://www.9cbs.com/read-2046.html

New Post(0)