After c++ friend declares the friend function, it still cannot access the private data of the class; the use of friend friend function

 wrong reason:

1. The friend function friend is misspelled (frined), or written outside the class

//friend void Other::Get_secret(People &ob);

class People
{
    //frined void Other::Get_secret(People &ob);
    friend void Other::Get_secret(People &ob);
private:
    char bust;
public:
    string name;
    int age;
    People(){}
};

2. A member function of a class, as a friend of another class

Private data bust in the People class,

The Ger_secret() function in the Other class wants to access the private data in the People class

Set the member function Get_secret() function as a friend of the People class

 Error solution:

1. At the beginning, the existence of class People should be declared forward

2. The Other class where the friend function is located is written first

3. The member function as a friend in the Other class is written outside the class (specifically behind the People class)

4. Write the member function in the Other class into the People class as a friend function

#include<iostream>
#include<string>
using namespace std;

class People;//1. 向前声明,只能说明类的名称
class Other   //2.友元成员函数所在类放在最上面
{
public:
    void Get_secret(People &ob);   //2.让这里的People可以是识别到
};


class People
{
    friend void Other::Get_secret(People &ob);     //4
private:
    char bust;
public:
    string name;
    int age;
    People(char bust,string name,int age){
        this->bust = bust;
        this->name = name;
        this->age = age;
    }

};

//3. 成员函数写在类外面
void Other::Get_secret(People &ob){
    cout<<"你好,你是:"<<ob.name<<" "<<"你的年龄是:"<<ob.age<<endl;
    cout<<"your bust is:"<<ob.bust<<endl;
}

int main()
{
    People lucy('A',"lucy",18);   //初始化lucy
    Other Tom;                    //Tom去访问lucy,包括lucy的私有数据
    Tom.Get_secret(lucy);

    return 0;
}

 Explanation :

1. The program runs from top to bottom. If the class People is not declared, the People in the other class function Get_secret(People &ob) will not be recognized.

2. If the People class is written at the top and the Other class is written at the bottom, it will cause the People class friend function other class to be unrecognizable, even if the class Other is declared earlier, the program cannot find the Get_secret member (running from top to bottom) .

3. If the Get_secret member function is written in the Other class, the program runs from top to bottom. The code declares the existence of the People class. The program does not know which data members are in the class, and an error is reported.

3. The entire class is used as a friend of another class

You only need to declare at the beginning, the position of the class as a friend is arbitrary, and the member function can be written in the class

#include<iostream>
#include<string>
using namespace std;

class Other;//1. 向前声明,只能说明类的名称


class People
{
    friend class Other;  
private:
    char bust;
public:
    string name;
    int age;
    People(char bust,string name,int age){
        this->bust = bust;
        this->name = name;
        this->age = age;
    }

};

class Other  
{
public:
    void Get_secret(People &ob);   

};

void Other::Get_secret(People &ob){
    cout<<"你好,你是:"<<ob.name<<" "<<"你的年龄是:"<<ob.age<<endl;
    cout<<"your bust is:"<<ob.bust<<endl;
}

int main()
{
    People lucy('A',"lucy",18);   //初始化lucy
    Other Tom;                    //Tom去访问lucy,包括lucy的私有数据
    Tom.Get_secret(lucy);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46290752/article/details/128552650