Use and functionality of friends (friend functions and friend classes) in C + + __jquery

Source: Internet
Author: User
Tags function definition modifier
After adopting the class mechanism, the data hiding and encapsulation are realized, the data members of the class are generally defined as private members, and the member functions are generally defined as common, which provides the communication interface between the class and the outside. However, there are times when you need to define functions that are not part of a class, but require frequent access to the data members of the class, and you can define these functions as friend functions for that function. In addition to the friend function, there are friends of the class, the two collectively referred to as friends. The role of the friend is to improve the efficiency of the program (that is, it takes time to reduce the type check and security checks, etc.), but it destroys the encapsulation and concealment of the class, allowing Non-members to access the private members of the 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 of a class, and it does not belong to any class, but it needs to be declared in the definition of a class by adding the 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 in the private part of the class, or it can be placed in the public domain, and they are indistinguishable, all of which indicate 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 invocation of a friend function is consistent with the way and principle that a general function is invoked.

Friend class :
All member functions of a friend class are friend functions of another class, and you can access hidden information in another class, including private and protected members.
When you want a class to be able to access a private member of another class, you can declare the class as a friend of another category. The statement format that defines a friend class is as follows:
Friend class class name;
Where: friend and class are keywords, and class names must be a defined class in the program.

For example, the following statement shows that Class B is a friend of Class A:
Class A
{
...
Public
Friend class B;
...
};
After the above description, all member functions of Class B are friend functions of Class A, which can access private members of Class A and protect members.

Note When using the friend class:
(1) The friend relationship cannot be inherited.
(2) The friend relation is one-way, does not have the exchange sex. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, to see if there is a corresponding declaration in the class.
(3) The friendship relationship is not transitive. 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 class

"The principle and application of multithreaded Programming in Windows environment" explains:

If the package of the class is likened to a wall, then the friend mechanism is like a door on the wall, those

To allow classes or functions to allow access to a generic class or a function that is inaccessible to a private property and side

Method. The friend mechanism causes the encapsulation of the class to be weakened, so use must be cautious.

Description of the Friend class

By describing a class outside as a friend in the definition of this class, the outside class becomes the "friend

Friends, that class can access the private data of this class.

Class Merchant

{

Private:

int M_mymoney;

int m_myroom;

... ...

Public:

Friend class lawyer;

Int Getmoney ();

... ...

};

Class lawyer

{

Private:

... ...

Public:

... ...

};

That class has access to your private data only if you give it to a friend of your class.

Describes a function as a friend of a class that can access private data and methods of this class. Set

The semantic method is in the definition of the class, with the keyword friend in front of the function name.

"The challenge Day C + +" explains this:

Before explaining what a friend is, let's explain why you need friends and friends:
Usually for ordinary functions, it is impossible to access the protection members of a class, and if you want to do so, you have to make the members of the class public (shared), but the problem with this is that any external function can access it without constraint, and C + + uses the friend modifier, You can allow some of the functions you set to operate on these protected data to avoid setting all class members to public and to protect data members to the maximum extent possible.
A friend can make a normal function direct access to the protection data of a class. Avoid the frequent invocation of class member functions, can save the processor overhead, improve the efficiency of the program, but the paradox is that even the maximum protection, but also destroy the package characteristics of the class, which is the disadvantage of friends , Now the CPU speed is getting faster today we do not recommend it, but it as a necessary knowledge of C + +, a complete component, we still need to discuss.
Declaring an ordinary math in a class, and then adding a friend modifier to it, the function becomes a friend of that class, and you can access all the members of that class.

Let's look at a piece of code to see how we use friends to access all the members of the class.

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author
#include <iostream>
using namespace Std;
Class Internet
{
Public
Internet (char *name,char *address)//Change to: Internet (const char *name, const char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
friend void shown (Internet &obj); The declaration of a friend function
Public://change to: Private
Char name[20];
Char address[20];
};
void shown (Internet &obj)//function definition, not written, void Internet::shown (Internet &obj)
{
cout<<obj.name<<endl; To access members of the Internet class
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Shown (a);
Cin.get ();
}
The code above is defined by the friend function, we have successfully accessed the protection member name of object A, and the friend function is not considered a member function of a class, it is simply a normal function declared as a class friend, so the definition part of the class outer function cannot be written as a void Internet::shown ( Internet &obj), this should be noted.

================================================================================

Instance:

Define a Class A and class B, each with a private integer member variable initialized by a constructor; Class A has a member function show (b &b) to print private member variables for A and B, respectively, by using the friend function and the friend class to implement this functionality.

the


#include <iostream>

using namespace std;
by the Ufida and friend functions
Class B;
Class A;
Void Show (a&, b&);

Class B
{
Private:
int tt;
Friend class A;
friend Void Show (a&, b&);

Public:
B (int temp = m): TT (temp) {}

};


Class A
{
Private:
int value;
friend Void Show (a&, b&);

Public:
A (int temp =]: Value (temp) {}

void Show (B &b)
{
  cout << value ;< Endl;
  cout << b.tt << Endl;
}
};

Void Show (a& A, b& B)
{
cout << a.value << Endl;
cout << B. TT << Endl;
}

int main ()
{
A;
b b;
A.show (b);
Show (A, b);

      return 0;
}

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.