鱼C论坛

 找回密码
 立即注册
查看: 1012|回复: 3

[已解决]为什么加了friend还是无法访问私有成员

[复制链接]
发表于 2020-11-10 22:13:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. using namespace std;

  3. class Building{
  4.         friend void Goodgay::visit2();    //这里声明了友元
  5. public:
  6.         string m_SettingRoom = "客厅";
  7. private:
  8.         string m_BedRoom = "卧室" ;
  9. };

  10. class Goodgay{
  11. public:
  12.         Goodgay();
  13.         void visit1();
  14.         void visit2();                  //visit2可以访问building私有成员
  15.         Building *build;
  16. };
  17. Goodgay::Goodgay(){
  18.         build = new Building;
  19.        
  20. }
  21. void Goodgay::visit1(){
  22.         cout<<"正在访问Building中的:"<<build->m_SettingRoom<<endl;
  23. }
  24. void Goodgay::visit2(){
  25.         cout<<"正在访问Building中的:"<<build->m_BedRoom<<endl;
  26. }
  27. void test01(){
  28.         Goodgay g;
  29.         g.visit1();
  30.         g.visit2();
  31.        
  32.        
  33. }
  34. int main(){
  35.         test01();
  36.        
  37.         system("pause");
  38.         return 0;
  39. }
复制代码
最佳答案
2020-11-12 09:57:18
本帖最后由 xieglt 于 2020-11-12 10:00 编辑
  1. #include <iostream.h>
  2. #include <string>
  3. #include <stdlib.h>

  4. using namespace std;

  5. class Goodgay;
  6. class Building;

  7. class Goodgay{
  8. public:
  9.         Goodgay();
  10.         ~Goodgay();
  11.         void visit1();
  12.         void visit2();                  //visit2可以访问building私有成员
  13.         Building * build;
  14. };

  15. class Building
  16. {
  17.         friend void Goodgay::visit2();    //这里声明了友元
  18. public:
  19.        //可以这样赋值
  20.         Building()
  21.         {
  22.                 m_SettingRoom = "客厅";
  23.                 m_BedRoom = "卧室";
  24.         }
  25. public:
  26.         string m_SettingRoom; // = "客厅";不能这么赋值
  27. private:
  28.         string m_BedRoom; // = "卧室" ;不能这么赋值
  29. };


  30. Goodgay::Goodgay()
  31. {
  32.         build = new Building;
  33.        
  34. }

  35. //注意释放内存
  36. ~Goodgay::Goodgay()
  37. {
  38.         delete build;
  39. }

  40. void Goodgay::visit1()
  41. {
  42.         //cout 不能输出 string 对象,要输出只能输出 string.c_str()
  43.         cout << "正在访问Building中的:" << build->m_SettingRoom.c_str() << endl;
  44. }

  45. void Goodgay::visit2()
  46. {
  47.         //cout 不能输出 string 对象,要输出只能输出 string.c_str()
  48.         cout << "正在访问Building中的:"<< build->m_BedRoom.c_str() << endl;
  49. }

  50. void test01(){
  51.         Goodgay g;
  52.         g.visit1();
  53.         g.visit2();
  54.        
  55.        
  56. }
  57. int main(){
  58.         test01();
  59.        
  60.         system("pause");
  61.         return 0;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-12 09:57:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 xieglt 于 2020-11-12 10:00 编辑
  1. #include <iostream.h>
  2. #include <string>
  3. #include <stdlib.h>

  4. using namespace std;

  5. class Goodgay;
  6. class Building;

  7. class Goodgay{
  8. public:
  9.         Goodgay();
  10.         ~Goodgay();
  11.         void visit1();
  12.         void visit2();                  //visit2可以访问building私有成员
  13.         Building * build;
  14. };

  15. class Building
  16. {
  17.         friend void Goodgay::visit2();    //这里声明了友元
  18. public:
  19.        //可以这样赋值
  20.         Building()
  21.         {
  22.                 m_SettingRoom = "客厅";
  23.                 m_BedRoom = "卧室";
  24.         }
  25. public:
  26.         string m_SettingRoom; // = "客厅";不能这么赋值
  27. private:
  28.         string m_BedRoom; // = "卧室" ;不能这么赋值
  29. };


  30. Goodgay::Goodgay()
  31. {
  32.         build = new Building;
  33.        
  34. }

  35. //注意释放内存
  36. ~Goodgay::Goodgay()
  37. {
  38.         delete build;
  39. }

  40. void Goodgay::visit1()
  41. {
  42.         //cout 不能输出 string 对象,要输出只能输出 string.c_str()
  43.         cout << "正在访问Building中的:" << build->m_SettingRoom.c_str() << endl;
  44. }

  45. void Goodgay::visit2()
  46. {
  47.         //cout 不能输出 string 对象,要输出只能输出 string.c_str()
  48.         cout << "正在访问Building中的:"<< build->m_BedRoom.c_str() << endl;
  49. }

  50. void test01(){
  51.         Goodgay g;
  52.         g.visit1();
  53.         g.visit2();
  54.        
  55.        
  56. }
  57. int main(){
  58.         test01();
  59.        
  60.         system("pause");
  61.         return 0;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-12 22:50:40 | 显示全部楼层

不可以在成员变量定义的时候就赋值吗 我看语法好像没问题(虽然我知道构造函数赋值更标准一些)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-12 22:57:09 From FishC Mobile | 显示全部楼层
Ryan_Li 发表于 2020-11-12 22:50
不可以在成员变量定义的时候就赋值吗 我看语法好像没问题(虽然我知道构造函数赋值更标准一些)

不能赋值吧。至少我的编译器不支持。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 19:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表