Friends (friend, friend, and friend member functions) C + +

Source: Internet
Author: User

In some cases, it is convenient to allow specific non-member functions to access private members of a class while still blocking general access. For example, overloaded operators, such as input or output operators, often require access to private data members of the class.

The Friend (frend) mechanism allows a class to grant access to its non-public members to a specified function or class, and a friend's declaration begins with a friend, which can only appear inside the class definition, where a friend declaration may appear anywhere in the class: The friend is not a member of the class that granted the friend relationship. So they are not affected by the access control that is part of their declaration. In general, it is a good idea to put friend declarations in groups to the beginning or end of a class definition.


1, friend function

A friend function is a function that is capable of accessing all members of a class, although it is not a class member function. Class grants its friend special access, so that the friend function can access all members of the class.

#include <iostream>using namespace Std;class a{public:friend void set_show (int x, A &a);      The function is the declaration of the friend function private:int data;}; void set_show (int x, A &a)  //friend function definition, in order to access the members of class A {    a.data = x;    cout << a.data << Endl;} int main (void) {class A a;set_show (1, A); return 0;}

2, Friend class

All member functions of a friend class are friend functions of another class and can access hidden information in another class, including private members and protected members. When you want a class to be able to access the private members of another class, you can declare the class as a friend of another class.

Notes on the Friend class:

(1) A friend relationship cannot be inherited.
(2) Friend relationship is one-way and not commutative. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, it depends on whether there is a corresponding declaration in the class.
(3) Friend relationship does not have transitive nature. If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding declaration in the class.

#include <iostream>using namespace Std;class a{public:    friend class C;                         This is the declaration of the Friend class private:    int data;}; Class C             //friend class definition, in order to access the members of class A {public:    void set_show (int x, A &a) {a.data = x; cout<<a.data<<endl;}} ; int main (void) {    Class A A;    Class C C;    C.set_show (1, a);    return 0;}

3, friend member function

Make the member function in class B A friend function of Class A so that the member function of Class B can access all members of Class A.

When using the Friend member function, you should pay attention to the mutual dependence between the friend declaration and the friend definition, in which class B must be defined, otherwise class A cannot designate a function of B as friend. However, this member function of Class B can be defined only after class A has been defined. more generally, you must define a class that contains member functions before you can set member functions as friends. On the other hand, you do not have to pre-declare classes and non-member functions to set them as friends.

#include <iostream>using namespace Std;class A;    When using a Friend member function, be aware of the interdependence between the friend declaration and the friend definition. This is the Declaration of class A class B{public:    void set_show (int x, A &a);             The function is a friend function of class A};class a{public:    friend void b::set_show (int x, A &a);   The function is the declaration of a friend member function private:    int data;    void Show () {cout << data << Endl;}}; void b::set_show (int x, A &a)       //The function can be defined only after class A is defined, after all, it is set as friend to access the member of Class A {    a.data = x;    cout << a.data << Endl;} int main (void) {    Class A A;    Class b b;    B.set_show (1, a);    return 0;}

Friend Summary:

A friend is available when it is necessary to allow certain non-member functions to access private members (and protected members) of a class while still blocking general access.

Advantages:

The flexibility to implement tasks that require access to private or protected members of several classes;

Facilitates mixed programming with other languages that do not support class concepts, such as C language, assembly, etc.;

The IO Stream Library of the C + + language can be used more naturally by using the friend function overloads.

Disadvantages:

A class that grants access to its non-public members to other functions or classes destroys the encapsulation of the class and reduces the reliability and maintainability of the class.


Resources:

1, "C + + Prime" The 4th edition of the 12th chapter class of Friends of the Section


Friends (friend, friend, and friend member functions) C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.