Put the code last
Function of friend function:
You can access the private properties of this class
But there is no "class" when defining outside the class:“
Therefore, the friend function does not belong to this class
Meaning of friend function:
c + + friend function_ Bussy's blog - csdn blog_ c + + friend function csdn
1. The meaning of friend existence
Why have friend functions? The existence of friends can share data resources, reduce system call overhead and improve operation efficiency. If funA wants to access the private member function in class B, it only needs to add a friend returntype funA (T) in the declaration of class B; Just. Friend is the identifier of the friend function, which tells the compiler that it is a friend function and does not have this pointer, but it can access the private member function of class B, the return type of the returntype function, the type of the t-input parameter, and the t-argument.
2. Advantages and disadvantages of friends:
Advantages: improve programming efficiency, improve operation efficiency, and express clearly and simply
Disadvantages: it destroys the encapsulation mechanism, so try not to use friends.
Function of friend class:
You can let friends access their private properties
#The role of pragma once
1.#pragmaonce what is the function of this macro?
In order to avoid the same header file being include d multiple times, there are two macro implementation methods in C/C + +: one is #ifndef method; The other is #pragma once.
There is not much difference between compilers that can support these two methods. But there are still some subtle differences between the two.
#Pragma once Usage Summary_ Program ape Lao Fan's blog - CSDN blog_# pragma once
You can try to look at my code
You can delete #pragma once
friend class A
And so on. Let's see where the error report is deleted
See for yourself how to solve the wrong content
This word has too much to type
first
friend.cpp
the second
friendA.h
Third
friendB.h
#include <iostream> #include "friendA.h" #include "friendB.h" using namespace std; int main() { A a(2,3,4); B b(4,5,6); cout<<ave(a)<<endl;//Not a.ave(a), this is a friend function cout<<a.aveB(b)<<endl; return 0; }
#pragma once #include <iostream> #include "friendB.h" using namespace std; class A{ private: int score1; int score2; public: int score3; friend int ave(A); //Note that there are classes in parentheses, which is a bit like the definition of function pointers //Friend functions can access private properties // friend void set(); //Assign values to members //I can use constructors //In terms of function, can we use friend functions //Friend function is not a method of class (cannot write::) //Then you can't access the private properties of the object of the class //Therefore, you need to use a class method that can access the private properties of the class object in the friend function //So it's more troublesome //So I used the constructor A(int a,int b,int c); int aveB(B b); //This is not a friend function, but an ordinary class method }; int ave(A a) { //Access to private properties return (a.score1+a.score2+a.score3)/3; //Find out if it's nothing special to write like this } int A::aveB(B b) { //Access to private properties return (b.score11+b.score22+b.score33)/3; //Find out if it's nothing special to write like this } // void set(A a) // { // // cin>>a.score1;// External functions cannot access private properties // // cin>>a.score2; // //So this method doesn't work // //So I'm going to comment it all out // //Ha ha ha // return; // } A::A(int a,int b,int c) { score1 = a; score2 = b; score3 = c; }
#pragma once #include <iostream> #include "friendA.h" using namespace std; class B{ private: int score11; int score22; public: int score33; B(int a,int b,int c); friend class A; //Take an ideal example of reality //If I agree with you as my friend, then I will allow you to use some of my items //For example, pens, erasers and so on (referring to member variables) (there is no function (similarly called friend function)) //But I can't use your things casually, because you haven't recognized me as your friend }; B::B(int a,int b,int c) { score11 = a; score22 = b; score33 = c; }