【C++】引用变量--4.将引用用于结构(核心+难点)

举报
王博Kings 发表于 2020/12/29 23:19:33 2020/12/29
【摘要】 本节内容不好理解,需要反复看!  目录 两个程序运行及结果  程序示例1: 运行结果: 程序示例1的变形 程序变形后运行结果 两次运行结果对比 程序示例1的解读 1.程序函数解读 1.1 set_pc()函数 1.2 accumulate(team, one) 函数**重点 2 为何要返回引用 3 返回引用需注意的问题 3.1 避免返回临时变量 ...

本节内容不好理解,需要反复看! 

目录

两个程序运行及结果 

程序示例1:

运行结果:

程序示例1的变形

程序变形后运行结果

两次运行结果对比

程序示例1的解读

1.程序函数解读

1.1 set_pc()函数

1.2 accumulate(team, one) 函数**重点

2 为何要返回引用

3 返回引用需注意的问题

3.1 避免返回临时变量

3.2 使用指针new返回引用

4 将const用于引用(左值,右值)


两个程序运行及结果 

程序示例1:


  
  1. //strc_ref.cpp -- 使用结构引用
  2. #include<iostream>
  3. #include<string>
  4. struct free_throws
  5. {
  6. std::string name;
  7. int made;
  8. int attempts;
  9. float percent;
  10. };
  11. void display(const free_throws & ft);
  12. void set_pc(free_throws & ft);
  13. free_throws & accumulate(free_throws & target, const free_throws & source);
  14. int main()
  15. {
  16. //部分初始化--剩余的设置为0
  17. free_throws one = { "Ifelsa Branch", 13, 14 };
  18. free_throws two = { "Andor Knott", 10, 16 };
  19. free_throws three = { "Minnie Max", 7, 9 };
  20. free_throws four = { "Whily Looper", 5, 9 };
  21. free_throws five = { "Long Long", 6, 14 };
  22. free_throws team = { "Throwgoods", 0, 0 };
  23. //没有初始化的
  24. free_throws dup;
  25. set_pc(one);
  26. display(one);
  27. accumulate(team, one);
  28. display(team);
  29. //
  30. display(accumulate(team, two));
  31. accumulate(accumulate(team, three), four);
  32. display(team);
  33. //use return value in assignment
  34. dup = accumulate(team, five);
  35. std::cout << "Displaying team:\n";
  36. display(team);
  37. std::cout << "Displaying dup after assignment:\n";
  38. display(dup);
  39. set_pc(four);
  40. //ill-advised assignment
  41. accumulate(dup, five) = four;
  42. std::cout << "Displaying dup after ill-advised assignment:\n";
  43. display(dup);
  44. return 0;
  45. }
  46. //使用const 然后显示出来
  47. void display(const free_throws & ft)
  48. {
  49. using std::cout;
  50. cout << "Name: " << ft.name << '\n';
  51. cout << " Made : " << ft.made << '\t';
  52. cout << "Attempts: " << ft.attempts << '\t';
  53. cout << "Percent: " << ft.percent << '\n';
  54. }
  55. //计算百分比
  56. void set_pc(free_throws & ft)
  57. {
  58. if (ft.attempts != 0)
  59. {
  60. ft.percent = 100.0f * float(ft.made) / float(ft.attempts);
  61. }
  62. else
  63. {
  64. ft.percent = 0;
  65. }
  66. }
  67. //注意这里返回的是引用 free_throws &
  68. free_throws & accumulate(free_throws & target, const free_throws & source)
  69. {
  70. target.attempts += source.attempts;
  71. target.made += source.made;
  72. set_pc(target);
  73. return target;
  74. }

运行结果:

程序示例1的变形

这里 不用引用


  
  1. free_throws accumulate(free_throws & target, const free_throws & source)
  2. {
  3. target.attempts += source.attempts;
  4. target.made += source.made;
  5. set_pc(target);
  6. return target;
  7. }

  
  1. //strc_ref.cpp -- 使用结构引用
  2. #include<iostream>
  3. #include<string>
  4. struct free_throws
  5. {
  6. std::string name;
  7. int made;
  8. int attempts;
  9. float percent;
  10. };
  11. void display(const free_throws & ft);
  12. void set_pc(free_throws & ft);
  13. free_throws accumulate(free_throws & target, const free_throws & source);
  14. int main()
  15. {
  16. //部分初始化--剩余的设置为0
  17. free_throws one = { "Ifelsa Branch", 13, 14 };
  18. free_throws two = { "Andor Knott", 10, 16 };
  19. free_throws three = { "Minnie Max", 7, 9 };
  20. free_throws four = { "Whily Looper", 5, 9 };
  21. free_throws five = { "Long Long", 6, 14 };
  22. free_throws team = { "Throwgoods", 0, 0 };
  23. //没有初始化的
  24. free_throws dup;
  25. set_pc(one);
  26. display(one);
  27. accumulate(team, one);
  28. display(team);
  29. //
  30. display(accumulate(team, two));
  31. accumulate(accumulate(team, three), four);
  32. display(team);
  33. //use return value in assignment
  34. dup = accumulate(team, five);
  35. std::cout << "Displaying team:\n";
  36. display(team);
  37. std::cout << "Displaying dup after assignment:\n";
  38. display(dup);
  39. set_pc(four);
  40. //ill-advised assignment
  41. accumulate(dup, five) = four;
  42. std::cout << "Displaying dup after ill-advised assignment:\n";
  43. display(dup);
  44. return 0;
  45. }
  46. //使用const 然后显示出来
  47. void display(const free_throws & ft)
  48. {
  49. using std::cout;
  50. cout << "Name: " << ft.name << '\n';
  51. cout << " Made : " << ft.made << '\t';
  52. cout << "Attempts: " << ft.attempts << '\t';
  53. cout << "Percent: " << ft.percent << '\n';
  54. }
  55. //计算百分比
  56. void set_pc(free_throws & ft)
  57. {
  58. if (ft.attempts != 0)
  59. {
  60. ft.percent = 100.0f * float(ft.made) / float(ft.attempts);
  61. }
  62. else
  63. {
  64. ft.percent = 0;
  65. }
  66. }
  67. //注意这里返回的是值传递 free_throws
  68. free_throws accumulate(free_throws & target, const free_throws & source)
  69. {
  70. target.attempts += source.attempts;
  71. target.made += source.made;
  72. set_pc(target);
  73. return target;
  74. }

程序变形后运行结果

两次运行结果对比

程序示例1的解读

(书第6版266页,我只是截取其中最为核心重点)

1.程序函数解读

1.1 set_pc()函数

1.2 accumulate(team, one) 函数**重点

2 为何要返回引用

3 返回引用需注意的问题

3.1 避免返回临时变量

3.2 使用指针new返回引用

4 将const用于引用(左值,右值)

 

文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。

原文链接:kings.blog.csdn.net/article/details/90523251

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。