(3) Friend functions and friend classes

1. The
concept of friend : through friends, the encapsulation of the class is broken, and all members in the class can be accessed

Category: friend function, friend class

2. Friend function
concept:

  1. The friend function is an ordinary function, not belonging to the class, but the friend relationship needs to be indicated in the class

  2. Friend function can access all members in the class, but the class cannot access the internal data of the friend function

format

  • When a friend function can be declared in a class, the keyword friend needs to be added
  • Friend functions can be declared outside the class, and defined outside the class. Need to add the keyword friend
class Cperson
{
    
    
private:
int age;
public:
friend void setPersonAge(Cperson& p,int age);
};
void setPersonAge(Cperson& p,int age) // 函数在类外声明和定义
{
    
    
p.age=age;
}
int main()
{
    
    
Cperson person;
setPersonAge(person,18);
}
class Cperson
{
    
    
private:
int age;
public:
friend void setPersonAge(Cperson& p,int age) // 友元函数在类内定义
{
    
    
p.age=age;
}
};

void setPersonAge(Cperson& p,int age)int main()
{
    
    
Cperson person;
setPersonAge(person,18);
}

Three, Tomomoto

  • The friend class is not a member of the class, and does not have this pointer

  • When a class A becomes a friend class of another class B, class A can access all members of class B

There are two forms of friend classes:
making the whole class a friend,
making a certain part of the function in the class a friend

Make the whole class friends

class Cb;        // 声明类
class Ca
{
    
    
private:
int num;
public:
friend class Cb; // 使整个类成为Ca的友元,则Cb所有的成员都可以访问Ca的所有成员
};

class Cb
{
    
    
public:
void setCaNum(Ca& a);
void Func();
};

Make a certain part of the function in the class friends

class Cb;                       // 声明类
class Ca
{
    
    
private:
int num;
public:
friend void Cb::setCaNum(Ca& a);// 只有Cb的setCaNum成为Ca的友元
};

class Cb
{
    
    
public:
void setCaNum(Ca& a);
void Func();
};

The friendship relationship cannot be inherited.
Insert picture description here
4. The characteristics of friendship

  • Unidirectional: Friends can access classes, but classes cannot access friends

  • Non-transitivity: A is a friend of B, C is a friend of A, but C is not a friend of B

  • No inheritance: There is an introduction in the above friend class.
    Insert picture description here
    5. The relationship between the declaration of the friend and the scope

  • When using a friend, consider the declaration and scope of its friend, please see the following code

class X
{
    
    
public:
friend void f(){
    
    }    // 友元函数在类内定义
X() {
    
     f(); }           // 错误,f()函数在下面才声明,此处检测不到
void g();
void h();
};
void X::g(){
    
     return f(); } // 错误,此时f()函数还没有被声明
void f();                  // 声明函数
void X::h(){
    
     return f(); } // 正确,检测到f()函数被声明

Guess you like

Origin blog.csdn.net/qq_40329851/article/details/114275054