"C + + Basics" Friend

Source: Internet
Author: User

Directory (?) [-]

    1. Overview
    2. Characteristics
    3. Realize
      1. 1 friend function
        1. 11 Declarations and definitions
        2. 12 Example
      2. 2 Friend Class
        1. 21 Declarations and definitions
        2. 22 Example
      3. Attention
        1. 1 friend relationship has no inheritance
        2. 2 friend relationships are not transitive
1. Overview

Friend provides a mechanism for accessing private or protected members of another class by a common function or class member function. In other words, there are two types of friends:

(1) Friend function: A normal function for a private or protected member that accesses a class.

(2) Friend class: A member function in Class A accesses a private or protected member in Class B.

2. Features

Advantages: Improve the operation efficiency of the program.

Disadvantage: It destroys the encapsulation of classes and the transparency of data.

3. Implement

3.1. Friend function 3.1.1. Declarations and definitions

Declared in any area of the class declaration, and the definition is outside the class.

[CPP]View Plaincopy
    1. Friend < type >< friend function name > (< parameter table >);
Note that the friend function is just a normal function, not a class member function of the class, it can be called anywhere, and the friend function accesses the private or protected members of the class through the object name.

3.1.2. Example

[CPP]View Plaincopy
  1. TestFriend.cpp
  2. #include "stdafx.h"
  3. Class A
  4. {
  5. Public
  6. A (int _a): A (_a) {};
  7. friend int geta_a (a &_classa); Friend function
  8. Private
  9. int A;
  10. };
  11. int Geta_a (a &_classa)
  12. {
  13. return _CLASSA.A; //Access private variables by object name
  14. }
  15. int _tmain (int argc, _tchar* argv[])
  16. {
  17. A _classa (3);
  18. Std::cout<<geta_a (_classa); the//friend function is just a normal function and can be called anywhere
  19. return 0;
  20. }

3.2. Friend class 3.2.1. Declarations and definitions

The declaration of a friend class is in the declaration of the class and is implemented outside of that class.

[CPP]View Plaincopy
    1. Friend class < friend category name >;
An instance of a friend class is defined in the main function.

3.2.2. Example

[CPP]View Plaincopy
  1. TestFriend.cpp
  2. #include "stdafx.h"
  3. Class B
  4. {
  5. Public
  6. B (int _b): b (_b) {};
  7. friend class C; Declare friend Class C
  8. Private
  9. int b;
  10. };
  11. Class C//Implement friend Class C
  12. {
  13. Public
  14. int Getb_b (b _classb) {
  15. return _classb.b; //Access private members of friend Class B
  16. };
  17. };
  18. int _tmain (int argc, _tchar* argv[])
  19. {
  20. B _CLASSB (3);
  21. C _CLASSC; //Define friend class instances
  22. Std::cout<<_classc.getb_b (_CLASSB);
  23. return 0;
  24. }

4. Note 4.1. There is no inheritance of the friend relationship

If Class B is a friend of Class A, Class C inherits from Class A, then the friend Class B is not able to directly access the private or protected members of Class C.

4.2. Friend relationships are not transitive

The addition of Class B is a friend of Class A, Class C is a friend of Class B, then the Friend Class C is not able to directly access the private or protected members of Class A, that is, there is no "friend of friends" this relationship.

Go

"C + + Basics" Friend

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.