C + + friend H5 bucket landlord chess source Full set of law detailed

Source: Internet
Author: User

In C + +, H5 bucket landlord chess source set (h5.super-mans.com q:2012035031) H5 bucket landlord chess source set, member functions are generally defined as public, in order to provide the communication interface between the class and the outside world. However, there are times when you need to define functions that are not part of a class, but that require frequent access to the data members of the class, you can define these functions as friend functions for that function. In addition to the friend function, there are friend classes, which are collectively referred to as friends. The role of friends is to improve the efficiency of the program's operation (that is, reducing the type checking and security checks and so on requires time overhead), but it destroys the encapsulation and concealment of the class, so that non-member functions can access the private members of the class.

Friend function:  
        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, just precede the name of the friend with the keyword friend, whose format is as follows:
       friend type function name (formal parameter) ;
        The declaration of a friend function can be placed in the private part of the class or in the public part, they are no different, it is a friend function of the class.
        A function can be a friend function of more than one class, only to be declared separately in each class. The invocation of the
        friend function is consistent with the way the general function is invoked and the principle.
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 a private member 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, 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
The principles and applications of multithreaded programming in Windows environments explains:
If the encapsulation of a class is likened to a wall, then the friend mechanism is like a wall opening a door, which has to be
to a class or function that allows access to a generic class or function that does not have access to private properties and parties
Method. The friend mechanism makes the encapsulation of the class weaken, so it must be used with caution.

Friend class:

A class in the outside world is described as friend in the definition of this class, then the outside class becomes the "friend of Friends" of this category, and that class can access the private data of this class.
Class Merchant
      {
          private:
      &N Bsp      int M_mymoney;
             int m_myroom;
             
          public:
             friend class Lawyer;
             int Getmoney ();
             
     };
      Class Lawyer
     {
        Private:
    &N Bsp     ...
        public:
         
     };
      The class has the right to access your private data only if you give it to a class as your friend. Demonstrates that a function is a friend function of a class that can access private data and methods of this class. The definition method is to add the keyword friend to the function name in the definition of the class.

The pros and cons of friend and friend are needed:

It is usually impossible to access the protection members of a class for normal functions, and if you want to do so you have to make the members of the class public (common), but the problem is that any external function can access it without any constraints, and C + + uses the friend modifier, You can let some of the functions you set up to operate on these protection data, avoid all members of the class to be public, to maximize the security of data members.

Friends can make the normal function directly access the protection data of the class, avoid the frequent call of class member function, can save the processor overhead and improve the efficiency of the program, but the contradiction is, even the maximum protection, also destroyed the package characteristics of the class, this is the disadvantage of friends, Now that the CPU is getting faster today we don't recommend using it, but as a necessary knowledge point for C + +, a complete component, we still need to discuss it. Declaring a common mathematics in a class, preceded by a friend modification, becomes a friend of that class and can access all members of that class.

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

[CPP]View PlainCopyprint?
  1. #include <iostream>
  2. Using namespace std;
  3. Class Internet
  4. {
  5. Public
  6. Internet (char *name,char *address) //Change to: Internet (const char *name, const char *address)
  7. {
  8. strcpy (Internet::name,name);
  9. strcpy (internet::address,address);
  10. }
  11. Friend void shown (Internet &obj); declaration of the//friend function
  12. Public: //change to: Private
  13. Char name[20];
  14. Char address[20];
  15. };
  16. void shown (Internet &obj) //out-of-class generic function definition, access <span style= "Color:rgb (--) font-family:" Microsoft Yahe I "; font-size:15px; " >a the protected member of the object Name</span>, cannot be written as, void Internet::shown (Internet &obj)
  17. {
  18. cout<<obj.name<<endl; //access to members in the Internet class
  19. }
  20. void Main ()
  21. {
  22. Internet A ("Google","http://www.google.cn/";);
  23. Shown (a);
  24. Cin.get ();
  25. }

Example 2:

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

[CPP]View PlainCopyprint?
    1. #include <iostream>
    2. Using namespace std;
    3. Class B;
    4. Class A;
    5. void Show (a&, b&);
    6. Class B
    7. {
    8. Private
    9. int tt;
    10. Friend class A;
    11. Friend void Show (a&, b&);
    12. Public
    13. B ( int temp = +): TT (temp) {}
    14. };
    15. Class A
    16. {
    17. Private
    18. int value;
    19. Friend void Show (a&, b&);
    20. Public
    21. A (int temp = $): Value (temp) {}
    22. void Show (B &b)
    23. {
    24. cout << value << Endl;
    25. cout << b.tt << Endl;
    26. }
    27. };
    28. void Show (a& A, b& B)
    29. {
    30. cout << a.value << Endl;
    31. cout << B. TT << Endl;
    32. }
    33. int main ()
    34. {
    35. A;
    36. b b;
    37. A.show (b);
    38. Show (A, b);
    39. return 0;
    40. }

C + + friend H5 bucket landlord chess source Full set of law detailed

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.