• C++核心编程——P36-友元


    友元

    客厅就是Public,你的卧室就是Private

    客厅所有人都可以进去,但是你的卧室只有和你亲密的人可以进。

    在程序中,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元技术。

    友元的目的就是让一个函数或者类 访问另一个类中的私有元素。

    友元的关键字——friend

    友元的三种实现

    • 全局函数做友元
    • 类做友元
    • 成员函数做友元
    1.全局函数做友元

             就是将此函数在类的最上面写一个声明,前面加一个friend。

    1. #include
    2. #include
    3. using namespace std;
    4. class Building
    5. {
    6. //goodgay全局函数是Building类的一个好朋友,可以访问你家的卧室(私有成员)
    7. friend void goodgay(Building* building);
    8. public:
    9. Building()
    10. {
    11. m_SittingRoom = "客厅";
    12. m_BedRoom = "卧室";
    13. }
    14. public:
    15. string m_SittingRoom;
    16. private:
    17. string m_BedRoom;
    18. };
    19. //全局函数
    20. void goodgay(Building* building)
    21. {
    22. cout << "好基友全局函数正在访问你的" << building->m_SittingRoom << endl;
    23. cout << "好基友全局函数正在访问你的" << building->m_BedRoom << endl;
    24. }
    25. void test()
    26. {
    27. Building building;
    28. goodgay(&building);
    29. }
    30. int main(void)
    31. {
    32. test();
    33. system("pause");
    34. return 0;
    35. }
    2.类做友元

           一个类在另一个中friend class xx。

    1. #include
    2. #include
    3. using namespace std;
    4. //在前面先声明一下
    5. class Building;
    6. class GoodGay
    7. {
    8. public:
    9. GoodGay();
    10. public:
    11. void visit();//参观函数 访问Building中的属性(公共属性和私有)
    12. Building* building;
    13. };
    14. class Building
    15. {
    16. //GoodGay是Building类的好朋友,可以访问其私有属性
    17. friend class GoodGay;
    18. public:
    19. Building();
    20. public:
    21. string m_SittingRoom;
    22. private:
    23. string m_BedRoom;
    24. };
    25. //在类外写成员函数
    26. Building::Building()
    27. {
    28. m_SittingRoom = "客厅";
    29. m_BedRoom = "卧室";
    30. }
    31. GoodGay::GoodGay()
    32. {
    33. //创建一个Building对象
    34. building = new Building;
    35. }
    36. void GoodGay::visit()
    37. {
    38. cout << "好基友正在访问你的" << building->m_SittingRoom << endl;
    39. cout << "好基友正在访问你的" << building->m_BedRoom << endl;
    40. }
    41. void test()
    42. {
    43. GoodGay gy;
    44. gy.visit();
    45. }
    46. int main(void)
    47. {
    48. test();
    49. system("pause");
    50. return 0;
    51. }
    52. //在你的代码中,building = new Building; 是在 GoodGay 类的构造函数 GoodGay::GoodGay() 中创建了一个 Building 类的对象,然后将其地址存储在 GoodGay 类的 building 成员变量中。
    53. 具体解释如下:
    54. GoodGay::GoodGay() 构造函数:
    55. 这是 GoodGay 类的构造函数,它在 GoodGay 类的对象被创建时调用。
    56. 在构造函数内部,building = new Building; 执行了动态内存分配操作,创建了一个新的 Building 类的对象,并返回该对象的地址。
    57. 这个地址被存储在 GoodGay 类的 building 成员变量中,因此 building 成员变量现在指向了一个新的 Building 对象。
    58. Building::Building() 构造函数:
    59. 这是 Building 类的构造函数,用于初始化 Building 类的对象。
    60. 在构造函数内部,m_SittingRoom 和 m_BedRoom 成员变量都被初始化为特定的字符串值。
    61. 所以,building = new Building; 的目的是在 GoodGay 对象被创建时,同时创建了一个关联的 Building 对象,以便 GoodGay 对象可以访问 Building 对象的成员变量,包括 m_SittingRoom 和 m_BedRoom。这种关系通常用于表示两个类之间的友好关系,允许一个类的成员函数访问另一个类的私有成员。在你的代码中,GoodGay 类被声明为 Building 类的好朋友(friend class GoodGay;),因此它可以访问 Building 类的私有成员。
    3.成员函数做友元

    告诉编译器 另一个类中的xx成员函数作为本类的好朋友,可以访问私有函数。

    1. #include
    2. #include
    3. using namespace std;
    4. class Building;
    5. class GoodGay
    6. {
    7. public:
    8. GoodGay();
    9. void visit();//可以访问Building中私有成员
    10. void visit1();//不可以访问Building中私有成员
    11. Building* builidng;
    12. };
    13. class Building
    14. {
    15. //告诉编译器 GoodGay类中的visit成员函数作为本类的好朋友,可以访问私有函数
    16. friend void GoodGay::visit();
    17. public:
    18. Building();
    19. public:
    20. string m_SittingRoom;
    21. private:
    22. string m_BedRoom;
    23. };
    24. Building::Building()
    25. {
    26. m_SittingRoom = "客厅";
    27. m_BedRoom = "卧室";
    28. }
    29. GoodGay::GoodGay()
    30. {
    31. builidng = new Building;
    32. }
    33. void GoodGay::visit()
    34. {
    35. cout << "visit正在访问" << builidng->m_SittingRoom << endl;
    36. cout << "visit正在访问" << builidng->m_BedRoom << endl;
    37. }
    38. void GoodGay::visit1()
    39. {
    40. cout << "visit1正在访问" << builidng->m_SittingRoom << endl;
    41. }
    42. void test()
    43. {
    44. GoodGay gg;
    45. gg.visit();
    46. gg.visit1();
    47. }
    48. int main(void)
    49. {
    50. test();
    51. system("pause");
    52. return 0;
    53. }

  • 相关阅读:
    2008-2020年数据上市公司高管团队异质性数据包含Stata代码
    MnasNet架构解析与复现-神经架构搜索
    【笔试刷题训练】day_17
    3 个好玩的前端开源项目「GitHub 热点速览」
    点云从入门到精通技术详解100篇-基于激光点云的道路目标检测(续)
    怎么输入文字自动配音?手把手教会你,仅需三个步骤
    redis 数据结构(二)
    卡尔曼滤波(Kalman Filter)C#测试
    小程序毕设作品之微信体育馆预约小程序毕业设计成品(6)开题答辩PPT
    Python字符串详解(包含长字符串和原始字符串)
  • 原文地址:https://blog.csdn.net/LDBH66/article/details/133089651