[C ++ basics 05] youyuan function and youyuan class, 05 yuan class

Source: Internet
Author: User

[C ++ basics 05] youyuan function and youyuan class, 05 yuan class

Youyuan is a common function or class defined outside the class, but it needs to be described in the class body. In order to distinguish it from the member functions of the class, the keyword friend is used before the description.

Youyuan is not a member function, but it can be a private member in the member class.

Youyuan is used to improve the program running efficiency. However, it destroys the encapsulation and hiding of classes and enables non-member functions to be private members of the category.

There are two forms of youyuan:

(1) friend functions: a common function accesses a private or protected member in a class.

(2) youyuan class: The member function in Class A, namely, the private or protected member in Class B.


Youyuan function:

Declarations are made in any region of the class declaration, while the definition is outside the class.

Friend <type> <youyuan Function Name> (<parameter table> );

Example:

Class A {public: A (int _ a): a (_ a) {}; friend int getA_a (A & _ classA); // youyuan function private: int a ;}; int getA_a (A & _ classA) {return _ classA. a; // access the private variable through the object name} int _ tmain (int argc, _ TCHAR * argv []) {A _ classA (3); std :: cout <getA_a (_ classA); // The youyuan function is only a common function and can call return 0 anywhere ;}
Note that:

(1) The youyuan function is not a class member function and does not have the this pointer.
(2) A friend meta function is a function outside the class, so its declaration can be placed in the private or public segment of the class and there is no difference.
(3) A friend function is a common function and is not a member function of the class. It can be called anywhere. The friend function accesses the private or protected member of the class through the object name.

Youyuan class:

It can be understood that all member functions of Class B are membership functions of Class A, and Class B is membership class of Class.

Friend class <youyuan class Name>;

Example:

Class girl; class boy {public: void disp (girl &) ;}; void boy: disp (girl & x) // The disp () function is a member function of the class boy, it is also a friend meta function like girl {cout <"girl's name is:" <x. name <", age:" <x. age <endl; // In the member function disp of the boy, the private variable of the girl is directly accessed using the girl object.} class girl {private: char * name; int age; friend boy; // declare that the class boy is a friend of the class girl };

Note that,Youyuan class has no inheritance and transmission
(1) No inheritance: if Class B is A member of Class A and Class C inherits from Class A, then Class B cannot directly protect the private or member of class C.
(2) No transmission: if Class B is A friend of Class A and Class C is A friend of Class B, then Class C cannot directly protect the private or protected members of Class, that is, there is no such relationship as "friends of friends.





How do I declare functions and object classes?

Class
{
Friend class A_friend_class;
Friend void A_friend_function (void );
};

Class A_friend_class
{
};

Void A_friend_function (void)
{
}

In the description of youyuan, the () is incorrect. The A youyuan function is A member function, which is indicated that the B youyuan function in the class can be directly called in the class.

A is wrong. You need to declare it in the class instead of A member function.

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.