Simple summary of C + + friend usage

Source: Internet
Author: User

In C + +, friend is the destruction of encapsulation, friend in the C + + grammar system is a relatively small point of knowledge, in the development used is not a lot. Friend's usage is sometimes easy to forget, and some traps often jump in. This article makes a brief summary.

The friend mechanism in C + + allows the non-public members of a class to be accessed by a class or function, and the friend is divided into three types by type: ordinary non-class member function as friend, class member function as friend, class as friend. Friends include the declaration of friend and the definition of friend. Friend declaration by default for extern, that is, the scope of a friend or friend function has been extended to include the scope of the class definition, so even if we define a friend function within the class is not related.

1. Common non-member function friend

This type of friend function is most common, usually an operator, such as an input and output operator, as shown below:

Opeclass.h#pragma onceclass opeclass{friend opeclass& operator << (const opeclass& XX);p Ublic:opeclass (void); Opeclass (int x,int y); ~opeclass (void);p rivate:int width;int height;};

Opeclass.cpp#include "OpeClass.h" Opeclass::opeclass (void) {width = 50;height = 50;} Opeclass::opeclass (int x,int y): Width (x), height (y) {}opeclass::~opeclass (void) {}opeclass& operator<< ( Const opeclass& XX) {Cout<<xx.width<<xx.height;return *this;}

2. Class as Friend

The class as a friend needs to be aware of the interdependence between the friend class and the original class, and if the function defined in the Friend class uses the private variable of the original class, then the header file of the original class definition needs to be included in the file defined by the friend class. However, in the definition of the original class (the class that contains the friend class declaration), the header file containing the friend class is not required.
In addition, you do not need to declare a friend class before the class definition, because the declaration of a friend class is itself a declaration.

A.h  #pragma once  #include <iostream>  using namespace std;  Class A  {  //friend class B;  If you do not write this sentence, a compilation error will appear public:  ~a (void);  A ();p rivate:    
A.cpp#include "A.h" A::a () {m_nitem = 3;} A::~a (void) {}
B.h#pragma once  class B  {public  :  b (void);  ~b (void);  int func ();  };  
B.cpp#include "StdAfx.h" #include "B.h"  #include "A.h"//must include A.h #include <iostream>b::b (void) {}b: : ~b (void) {}int b::func ()  {  cout<< "This was in B" <<endl;  A A;return A.m_nitem;}  
3. class member functions as friend functions

This is slightly more complicated because you want the class member function as a friend, you use the class qualifier when declaring the friend, so you must first define the class that contains the friend function, but you must define the original class in advance when you define the function of the friend. It is common practice to define classes that contain friend functions, and then define the original class, which is not a messy order. (If this is a friend class, this is not required) as shown below:

A.h#pragma once#include "B.h" class A{friend int b::func (A xx);p ublic:a (void): MX (x), my ({) {}~a (void) {}private:int Mx;int my;};
B.h#pragma onceclass a;class b{public:b (void); ~b (void); int func (A xx);};
B.cpp#include "B.h" #include "A.h" b::b (void) {}b::~b (void) {}int b::func (A xx) {return xx.mx * xx.my;}
Main.cpp#include "A.h" #include "B.h" #include <iostream>using namespace std;void main () {a A; B B;cout<<b.func (a) <<endl;system ("pause");

4. Friends do not have the reciprocity, only has the individual nature

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.

5. Friends cannot be inherited

B is a friend class, C is a subclass of B, can't push out C is a friend

6. Friends do not have transitivity

B is a friend, C is a friend of B, can't push out C is a friend

7. Tips for using friend functions

When implementing a singleton pattern in C + +, you can instantiate an object with a friend function. Then the constructors and destructors of the class are designed as private functions.

Class Cmysingleton{public:    friend cmysingleton& Instancemec ();p rivate:    Cmysingleton () {};    Cmysingleton (const Cmysingleton &lxsington) {};    ~cmysingleton () {};}; cmysingleton& Instancemec () {    ///Because the function Instancemec () is a friend function of class Clxsingletonmec, all member functions of the class can be accessed. So there's no compile error    . Static Cmysingleton Instance;     




Simple summary of C + + friend usage

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.