在类内的公有权限区域使用friend关键字声明 友元函数
    在类外进行友元函数的定义

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. class Student{
    5. public:
    6. friend void getData(Student &student); // 在公有权限区域声明友元函数
    7. string school = "CTGU";
    8. Student(){
    9. name = "yxr";
    10. age = 18;
    11. }
    12. ~Student(){
    13. cout << "析构函数的调用" << endl;
    14. }
    15. private:
    16. string name;
    17. int age;
    18. };
    19. void getData(Student &student){
    20. cout << student.school << endl;
    21. cout << student.name << endl;
    22. cout << student.age << endl;
    23. }
    24. int main(){
    25. Student student;
    26. getData(student);
    27. system("pause");
    28. return 0;
    29. }