How to set access permissions for friend classes?

How to set access permissions for friend classes?

Friend class has unlimited access to all elements of the class

#include <iostream>    
#include <string>    
#include <algorithm>    
using namespace std;  
  
class Student;  
  
class Person  
{  
    friend class Student; // 显然友元类可以访问Person类中的所有成员(public,private,protected)    
private:  
    int mark;  
    string name;  
public:  
    Person() = default;  
    Person(const int& mark, const string& name)  
    {  
        this->mark = mark;  
        this->name = name;  
    }  
    Person& operator = (Person&& obj)  
    {  
        this->mark = obj.mark;  
        this->name = obj.name;  
        return *this;  
    }  
};  
  
class Student  
{  
private:  
    Person obj;  
public:  
    Student() = default;  
    Student(const int& mark, const string& name) // const引用允许接受右值    
    {  
        obj = { mark,name }; // 先生成匿名对象,然后调用重载赋值运算符赋值给obj    
    }  
    void ShowInf()  
    {  
        cout << obj.name << "的成绩是" << obj.mark << endl;    
    }  
};  
  
int main()  
{  
    Student obj(99, "张三");  
    obj.ShowInf();  
}  

 

The above example shows that if we do not impose restrictions on the friend class, the friend class will completely destroy the encapsulation, making the encapsulation of the class virtual, and completely free to access all members of the class.

We know that: the access rights of friend functions are limited to the scope of the class where the friend declaration is located, that is, as shown in the above example, the scope of the Student friend class is limited to the Person class domain.

We thought: How can we restrict access to the friend class?

Access permission settings for friend classes

#include <iostream>  
#include <string>  
#include <algorithm>  
using namespace std;  
  
class Student;  
  
class Person  
{  
    //friend class Student; // 显然友元类可以访问Person类中的所有成员(public,private,protected)  
private:  
    int mark;  
    string name;  
    char age;  
    string schoolname;  
public:  
    class subclass  
    {  
        friend class Student;  
    private:  
        int mark;  
        string name;  
    public:  
        subclass() = default;  
        subclass(const int& mark, const string& name)  
        {  
            this->mark = mark;  
            this->name = name;  
        }  
        subclass& operator = (subclass&& obj)  
        {  
            this->mark = obj.mark;  
            this->name = obj.name;  
            return *this;  
        }  
        void ShowInf()  
        {  
            cout << this->name << "的成绩是" << this->mark << endl;  
        }  
    } subobj;  
public:  
    Person() = default;  
    Person(const int& mark,const string& name,const char& age,const string& schoolname)  
    {  
        this->mark = mark;  
        this->name = name;  
        subobj = { mark,name }; // 匿名对象的赋值操作一定要使用右值引用  
        this->age = age;  
        this->schoolname = schoolname;  
    }  
    Person& operator = (Person&& obj)  
    {  
        this->mark = obj.mark;  
        this->name = obj.name;  
        this->age = obj.age;  
        this->subobj = move(obj.subobj);  
        this->schoolname = obj.schoolname;  
        return *this;  
    }  
};  
  
class Student  
{  
private:  
    Person obj;  
public:  
    Student() = default;  
    Student(const int& mark, const string& name, const char& age, const string& schoolname) // const引用允许接受右值  
    {  
        obj = { mark,name,age,schoolname }; // 先生成匿名对象,然后调用重载赋值运算符赋值给obj  
    }  
    void ShowInf()  
    {  
        //cout << obj.name << "的成绩是" << obj.mark << endl;  
        obj.subobj.ShowInf();  
    }  
};  
  
int main()  
{  
    Student obj(99, "张三", 'f', "水城中学");;  
    obj.ShowInf();  
}  

 

As shown in the above example, the method of restricting the access permission of the friend class is as follows:

 

 

Why declare data members of subclass subclass as a public permission type instead of a private permission type?

The Student class is not a friend function of the Person class. There is no way to break the encapsulation. The Student class is a friend class of the members of the Person.subclass subclass. It can break the encapsulation of the members of the Person.subclass subclass. Therefore, declare the subclass subclass members as public attribute data. Members actually provide data access interfaces artificially. In the Student class and any place where the Person class object is defined, you can access the subclass subclass as you want, but only the Student class can break the encapsulation of the subclass class members to access all the data in the subclass.

 

Guess you like

Origin blog.csdn.net/weixin_45590473/article/details/111569801