Analyze the Friends function and friend class _c language decorated by the friend keyword in C + + programming

Source: Internet
Author: User

In some cases, it is more convenient to grant member-level access to functions that are not members of a class or to all functions in a separate class. Class-only implementations can declare their friends. A function or class cannot declare itself as a friend of any class. In the class declaration, use the Friend keyword and the name of a non-member function or other class to allow it to access the private and protected members of your class.
Grammar

  Friend Class-name;
Friend Function-declarator;

Friend Declaration
If you declare a friend function that was not previously declared, the function is exported to a closed, non-class scope.
A function declared in a friend declaration is considered to have been declared with an extern keyword. (For more information about extern, see the static storage class descriptor.) )
Although functions with global scope can be declared as friend functions before their prototypes, member functions cannot be declared as friend functions until their full class declarations appear. The following code demonstrates the reason for this failure:

Class forwarddeclared; Class name is known.
Class Hasfriends
{
 friend int forwarddeclared::isafriend ();///C2039 error expected
};

The preceding example enters the class name forwarddeclared into the scope, but the complete declaration (specifically, the part that declares the function Isafriend) is unknown. Therefore, the Hasfriends declaration in the friend class generates an error.
to declare two classes that are friends with each other, you must specify the entire second class as a friend of the first class. This limitation is due to the fact that the compiler has enough information to declare the individual friend functions only at the position where the second class is declared.
Note
Although the entire second class must be a friend of the first class, you can choose which of the functions in the first class will be friends of the second class. The
friend function
friend function is a function that is not a member of a class, but it can access the private and protected members of the class. Friend functions are not considered class members; they are ordinary external functions that have special access rights. Friends are not within the scope of a class, unless they are members of another class, the member selection operator (.) is not used. and –>) call them. The friend function is declared by a class that grants access. You can place a friend declaration anywhere in the class declaration. It is not affected by the access control keyword. The
The following example displays the point class and the friend function Changeprivate. The friend function can access the private data members of the point object that it accepts as a parameter.

Friend_functions.cpp
//compile with:/EHsc
#include <iostream>

using namespace std;
Class Point
{
 friend void Changeprivate (Point &);
Public: Point
 (void): M_i (0) {}
 void printprivate (void) {cout << m_i << Endl;}

Private:
 int m_i;
};

void Changeprivate (Point &i) {i.m_i++;}

int main ()
{point
 spoint;
 Spoint.printprivate ();
 Changeprivate (spoint);
 Spoint.printprivate ();
output:0
   1
}
Class members as Friends
A class member function can be declared as a friend in another class. Take a look at the following example:
Classes_as_friends1.cpp
//compile with:/C
class B;
Class A {public
:
 int Func1 (b& B);
Private:
 int Func2 (b& B);
Class B {
private:
 int _b;
 A::FUNC1 is a friend function to Class B
 //So A::FUNC1 has access to all members of B
 friend int a::func1 (b &);
int a::func1 (b& b) {return b._b}//OK
int a::func2 (b& b) {return b._b;}//C2248

In the previous example, only the function a::func1 (b&) was granted friend access to class B. Therefore, access to the private member _b is correct in class Func1, but is not correct in FUNC2.
A friend class is a class in which all its member functions are friend functions of a class, that is, its member functions have access to private members of the class and protected members. Suppose the B declaration in class friend is:

Friend class A;

In this case, the Friend access rights to class B are granted for all member functions in class A. The following code is an example of a friend class:

Classes_as_friends2.cpp
//compile with:/EHsc
#include <iostream>

using namespace std;
Class YourClass {
friend class Yourotherclass;//Declare A friend class public
:
 yourclass (): Topsecret (0) {}
 void Printmember () {cout << topsecret << Endl;}
Private:
 int topsecret;
};

Class Yourotherclass {public
:
 void Change (yourclass& yc, int x) {yc.topsecret = x;}
};

int main () {
 yourclass yc1;
 Yourotherclass Yoc1;
 Yc1.printmember ();
 Yoc1.change (YC1, 5);
 Yc1.printmember ();
}

The friend relationship is not mutual unless it is explicitly specified. In the example above, YourClass member functions cannot access private members of the Yourotherclass.
A managed type cannot have any friend function, friend class, or friend interface.
A friend relationship cannot be inherited, which means that classes derived from Yourotherclass cannot access private members of YourClass. The friend relationship is not transitive, so the Yourotherclass friend class cannot access the private members of the YourClass.
The following illustration shows 4 class declarations: Base, Derived, Afriend, and Anotherfriend. Only the class Afriend has direct access to the private member of base (and all members that the base might have inherited).

The

inline friend definition
defines a friend function in the class declaration. These functions are inline functions, similar to member inline functions, behave as if they were defined when all class members are displayed but before the class scope closes (the end of the class declaration). The friend functions defined in the
class declaration are not considered to be within the bounds of the enclosing class;

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.