Friends in C + +

Source: Internet
Author: User
Tags class definition

Reprinted from:http://blog.chinaunix.net/uid-790245-id-2037327.html

Questions raised:

We already know that classes have the properties of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as public, this destroys the hidden features. In addition, it should be seen that in some cases, especially when multiple calls are made to certain member functions, time overhead is required due to parameter passing, type checking, and security checking, which affects how efficiently the program runs.

In order to solve the above problems, a scheme is proposed to make the UF element. A friend is a normal function that is defined outside the class, but he needs to be described in the class, in order to distinguish it from the member functions of the class, to be the keyword friend before the description. A friend is not a member function, but he can access a private member in a class. The role of friends is to improve the efficiency of the program, but he destroys the encapsulation and concealment of the class, allowing non-member functions to access the private members of the class.

A friend can be a function, which is called a friend function, and a friend can also be a class, which is called a friend class.

Friend function:

A friend function is characterized by the ability to access a non-member function of a private member of a class. The friend function is syntactically identical to the normal function, which is the same as the normal function on the definition and the call. The following example illustrates the application of a friend function.

1#include <iostream>2#include <cmath>3 using namespacestd;4 class Point5 {6  Public:7Point (DoubleXxDoubleyy) {x=xx; y=yy;}8FriendDoubleDistance (Point &a, Point &b);9 Private:Ten       Doublex, y; One }; A  - DoubleDistance (Point &a, Point &b) - { the     DoubleDX = a.x-b.x; -     DoubleDY = a.y-b.y; -     returnsqrt (dx*dx+dy*dy); - } +  - intMain () + { APoint P1 (3.0,4.0), p2 (6.0,8.0); at     DoubleD =Distance (P1, p2); -cout<<"Distance is"<<Endl; -     return 0; -}

Description: A friend function distance () is described in the Point class in the program, and he adds the friend keyword in front of the description, identifying that he is not a member function, but a friend function. His definition method is the same as the normal function definition, and differs from the definition of the member function because he does not need to indicate the class to which it belongs. However, he is able to refer to private members in a class, where a.x,b.x,a.y,b.y are private members of the class, and they are referenced by objects. When you call the META function, it is the same as the normal function, not called as a member function.

Friend class:

Friend In addition to the previous function, friend can also be a class, that is, a class can be a friend of another class. When a class is a friend of another class, this means that any member function of the class is a friend function of another class.

------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------- ------------------

friend function :
A friend function is a non-member function that can directly access a private member of a class. It is a normal function that is defined outside the class, it does not belong to any class, but it needs to be declared in the definition of the class, simply by adding a keyword friend to the name of the friend, in the following format:
Friend type function name (formal parameter);

The declaration of a friend function can be placed either in the private part of the class or in the public part, which is no different, and is a friend function of the class.
A function can be a friend function of more than one class, and it needs to be declared separately in each class.
The call of the friend function is consistent with the method and principle of calling the general function.

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. The statement format that defines the friend class is as follows:
friend class name;
Where: friend and class are keywords, and the class name must be a class that has already been defined in the program.

For example, the following statement illustrates that Class B is a friend class of Class A:
class A
{
.....
Public
friend class B;
.....
      };
after the above instructions, all member functions of Class B are friend functions of Class A, which can access the private and protected members of Class A.

Note When using friend classes:
(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, but also to see if there is a corresponding declaration in the class

Precautions:

1. Friends can access private members of the class.

2. Only within a class definition, a friend declaration can be placed anywhere in the class, typically at the beginning or end of a class definition.

3. A friend can be a normal non-member function, or a member function of another class previously defined, or an entire class.

4. The class must declare each function in the overloaded function set to be a friend.

5. A friend relationship cannot inherit, and the friend of the base class does not have special access rights to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access rights. The derived class of the base class cannot access the class that grants the friend relationship.

Friends in C + +

Related Article

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.