C ++ Primer study note _ 21 _ class and data abstraction (7) _ youyuan function and youyuan class, _ 21 yuan class

Source: Internet
Author: User

C ++ Primer study note _ 21 _ class and data abstraction (7) _ youyuan function and youyuan class, _ 21 yuan class

C ++ Primer study note _ 21 _ class and data abstraction (7) _ youyuan function and youyuan class

I. youyuan

1. youyuan Introduction

We know that member functions of the class can access other member functions of the same type, including public, private, and protected members. The external function of the class can only be a public member of the category class.

Youyuan is a mechanism that allows non-public members of the callback class of non-class member functions. The statement of youyuan starts with the keyword "friend", but it can only appear within the class definition. The youyuan statement can appear anywhere in the class.
You can specify a function as a friend of a class, or specify the entire class as a friend of another class.

-- Youyuan Function

-- Youyuan class


[Best practices]

Generally, it is a good idea to group the comments to the beginning or end of the class definition!

Assume that a window management class Window_Mgr may need to access the internal data of the Screen Object managed by it. Screen should allow it to access its own private members:

class Screen{    friend class Window_Mgr;};

Window_Mgr members can directly reference Screen private members:

Window_Mgr &Window_Mgr::relocate(Screen::index x,Screen::index c,Screen &s){    s.height += r;    s.width += c;    return *this;}

If a friend declaration is missing, this code will fail: the height and width members of the form parameter s are not allowed.


2. youyuan declaration and scope

Pay attention to the mutual dependency between the AU $ statement and the definition of AU $. In the previous example, the class Window_Mgr must be defined first. Otherwise, the Screen class cannot specify a Window_Mgr function as a friend. However, the relocate function can be defined only after the Screen class is defined.

Generally, you must first define a class that contains a member function to set the member function as a friend.

The youyuan declaration introduces named classes or non-member functions to the peripheral scope. In addition, you can define the function within the class, and the function scope is extended to the scope that surrounds the class definition.


3. Friend Functions

A friend function is defined outside the class scope, but it must be described in the class body.
To be different from the member functions of this class, the function is defined by using the keyword "friend" in the class. The format is as follows:

Friend function name of the friend type (parameter table );

Youyuan's role is to improve program running efficiency


Precautions for functions:

(1) If you are not a member function of a class, you must use the object name plus operator "." To access the object in the function body and add the object member name. However, you can use a function to evaluate all the members of the class (public, private, and protected). Generally, a function can only be a public member of the function.
(2) The Meta function is not restricted by the access permission keyword in the class. You can place it in the public, private, and protected part of the class, but the result is the same.
(3) The scope of a type of membership function is not the scope of this category. If the membership function is another type of member function, its scope is another type of scope, otherwise it is the same as the general function.
(4) youyuan function destroys the encapsulation of the object-oriented programming class. Therefore, if you do not have to use youyuan function, try to use it as little as possible. Or use other methods to guarantee encapsulation.


[Example]

#include <math.h>#include <iostream>using namespace std;class Point{    friend double Distance(const Point &p1, const Point &p2);public:    Point(int x, int y);private:    int x_;    int y_;};Point::Point(int x, int y) : x_(x), y_(y){}double Distance(const Point &p1, const Point &p2){    double dx = p1.x_ - p2.x_;    double dy = p1.y_ - p2.y_;    return sqrt(dx * dx + dy * dy);}int main(void){    Point p1(3, 4);    Point p2(6, 9);    cout << Distance(p1, p2) << endl;    return 0;}
Result: 5.83095

Explanation: Distance in a program is a Point-class friend function, which can be a private data member of the vertex class.


4. youyuan

If the member functions of A Class B frequently access the data members of another class A, and the Private/Protectd limitation of the data members of A causes the trouble of B's access, B can only perform indirect access through the Public member functions of.

In this case, B can be made into A class A's friend Meta class, that is, Class A opens its Private/Protectd content to Class B, allowing B to directly access it.
Youyuan class: one class can be used as the youyuan of another class.
-- All member functions of the youyuan class are the youyuan functions of another class.
Statement of the youyuan class:
Friend class name;


Youyuan considerations:

(1) the relationship between friends and Yuan is one-way.
(2) Friendship cannot be passed
(3) Friendship cannot be inherited


TeleController. h:

#include <iostream>using namespace std;#ifndef  _TELE_CONTROLLER_H_#define _TELE_CONTROLLER_H_class Television;class TeleController{public:        void VolumeUp(Television &tv)        {                tv.volume_ += 1;        }        void VolumeDown(Television &tv)        {                tv.volume_ -= 1;        }        void ChanelUp(Television &tv)        {                tv.chanel_ += 1;        }        void ChanelDown(Television &tv)        {                tv.chanel_ -= 1;        }        void Print(Television &tv)        {                cout << "chanel_: " << tv.chanel_ << endl;                cout << "volume_: " << tv.volume_ << endl;        }};#endif // _TELE_CONTROLLER_H_

Television. h:

#ifndef _TELEVISION_H_#define _TELEVISION_H_class TeleController;class Television{    friend class TeleController;public:    Television(int volume, int chanel): volume_(volume), chanel_(chanel) {}private:    int volume_;    int chanel_;};#endif // _TELEVISION_H_

Test. cpp

#include "Television.h"#include "TeleController.h"#include <iostream>using namespace std;int main(void){    Television tv(1, 1);    TeleController tc;    tc.VolumeUp(tv);    tc.Print(tv);    return 0;}

Running result:

Chanel _: 1
Volume _: 2

Explanation: The TeleController class is used as the friend Meta class of the Television class, so that the member functions of the TeleController class can access all the members of the Television class, including private.



Refer:

C ++ primer version 4
Valid tive C ++ 3rd

Http://blog.csdn.net/jnu_simba/article/details/9284777

Http://blog.csdn.net/zjf280441589/article/details/24838175
C ++ programming specifications


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.