C + + Learning Note 5

Source: Internet
Author: User

1. Friends

1. Define who friend, in whose class you declare the friend you want to define with the keyword friend.

2. Some functions are not part of a class, but frequently access data members of a class, which can be defined as friend functions or friend classes.

3. Friends can only occur between heterogeneous, with no privacy between the peers.

4. Function: Improve the operation efficiency of the program, but destroy the encapsulation and concealment between classes, so that non-member functions can access the private members of the class

5. Friend Purpose essence: Do not allow other members that are not part of this class to become members of the class and have properties of this class member.

6. The friend whose name is declared, it can access its private member through its object "defines a friend of B as" A, the private member of B can be accessed through the object of B "

7. Friend function without this pointer

8. Precautions:

1. A friend relationship cannot be inherited.

2. A friend relationship is one-way and does not have a commutative nature. "A is a friend of B, B is not a friend of a"

3. Friend relationships do not have transitivity. "C is a friend of B, B is a friend, but C is not a friend of a"

2. When defining a friend function, use a forward declaration.

Forward declaration: is an incomplete declaration that simply provides the class name (without providing a class implementation). But there are limitations:

1. Cannot define objects of class

2. Can be used to define pointers or references to this type

3. Use this type for declaration (not definition) as a formal parameter type or function return type.

The implementation process of the friend function:

If a function of a class (a) is declared as a friend function of another class (b), then class A is defined before Class B, and Class B is preceded by a forward declaration, and if a friend function has a member operation in class B, the function can only be declared in Class A, but now after Class B is defined.

The realization of a friend class: A friend class can only be declared in a class. All functions in its class are friend functions.

C + + Learning Note 5

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.