【C++】引用变量--4.将引用用于结构(核心+难点)
【摘要】 本节内容不好理解,需要反复看!
目录
两个程序运行及结果
程序示例1:
运行结果:
程序示例1的变形
程序变形后运行结果
两次运行结果对比
程序示例1的解读
1.程序函数解读
1.1 set_pc()函数
1.2 accumulate(team, one) 函数**重点
2 为何要返回引用
3 返回引用需注意的问题
3.1 避免返回临时变量
...
本节内容不好理解,需要反复看!
目录
1.2 accumulate(team, one) 函数**重点
两个程序运行及结果
程序示例1:
-
//strc_ref.cpp -- 使用结构引用
-
#include<iostream>
-
#include<string>
-
struct free_throws
-
{
-
std::string name;
-
int made;
-
int attempts;
-
float percent;
-
};
-
-
void display(const free_throws & ft);
-
void set_pc(free_throws & ft);
-
free_throws & accumulate(free_throws & target, const free_throws & source);
-
-
int main()
-
{
-
//部分初始化--剩余的设置为0
-
free_throws one = { "Ifelsa Branch", 13, 14 };
-
free_throws two = { "Andor Knott", 10, 16 };
-
free_throws three = { "Minnie Max", 7, 9 };
-
free_throws four = { "Whily Looper", 5, 9 };
-
free_throws five = { "Long Long", 6, 14 };
-
free_throws team = { "Throwgoods", 0, 0 };
-
//没有初始化的
-
free_throws dup;
-
-
set_pc(one);
-
display(one);
-
accumulate(team, one);
-
display(team);
-
-
//
-
display(accumulate(team, two));
-
accumulate(accumulate(team, three), four);
-
display(team);
-
-
//use return value in assignment
-
dup = accumulate(team, five);
-
std::cout << "Displaying team:\n";
-
display(team);
-
std::cout << "Displaying dup after assignment:\n";
-
display(dup);
-
set_pc(four);
-
-
//ill-advised assignment
-
accumulate(dup, five) = four;
-
std::cout << "Displaying dup after ill-advised assignment:\n";
-
display(dup);
-
return 0;
-
}
-
-
//使用const 然后显示出来
-
void display(const free_throws & ft)
-
{
-
using std::cout;
-
cout << "Name: " << ft.name << '\n';
-
cout << " Made : " << ft.made << '\t';
-
cout << "Attempts: " << ft.attempts << '\t';
-
cout << "Percent: " << ft.percent << '\n';
-
}
-
-
//计算百分比
-
void set_pc(free_throws & ft)
-
{
-
if (ft.attempts != 0)
-
{
-
ft.percent = 100.0f * float(ft.made) / float(ft.attempts);
-
}
-
else
-
{
-
ft.percent = 0;
-
}
-
}
-
-
//注意这里返回的是引用 free_throws &
-
free_throws & accumulate(free_throws & target, const free_throws & source)
-
{
-
target.attempts += source.attempts;
-
target.made += source.made;
-
set_pc(target);
-
return target;
-
}
运行结果:
程序示例1的变形
这里 不用引用
-
free_throws accumulate(free_throws & target, const free_throws & source)
-
{
-
target.attempts += source.attempts;
-
target.made += source.made;
-
set_pc(target);
-
return target;
-
}
-
//strc_ref.cpp -- 使用结构引用
-
#include<iostream>
-
#include<string>
-
struct free_throws
-
{
-
std::string name;
-
int made;
-
int attempts;
-
float percent;
-
};
-
-
void display(const free_throws & ft);
-
void set_pc(free_throws & ft);
-
free_throws accumulate(free_throws & target, const free_throws & source);
-
-
int main()
-
{
-
//部分初始化--剩余的设置为0
-
free_throws one = { "Ifelsa Branch", 13, 14 };
-
free_throws two = { "Andor Knott", 10, 16 };
-
free_throws three = { "Minnie Max", 7, 9 };
-
free_throws four = { "Whily Looper", 5, 9 };
-
free_throws five = { "Long Long", 6, 14 };
-
free_throws team = { "Throwgoods", 0, 0 };
-
//没有初始化的
-
free_throws dup;
-
-
set_pc(one);
-
display(one);
-
accumulate(team, one);
-
display(team);
-
-
//
-
display(accumulate(team, two));
-
accumulate(accumulate(team, three), four);
-
display(team);
-
-
//use return value in assignment
-
dup = accumulate(team, five);
-
std::cout << "Displaying team:\n";
-
display(team);
-
std::cout << "Displaying dup after assignment:\n";
-
display(dup);
-
set_pc(four);
-
-
//ill-advised assignment
-
accumulate(dup, five) = four;
-
std::cout << "Displaying dup after ill-advised assignment:\n";
-
display(dup);
-
return 0;
-
}
-
-
//使用const 然后显示出来
-
void display(const free_throws & ft)
-
{
-
using std::cout;
-
cout << "Name: " << ft.name << '\n';
-
cout << " Made : " << ft.made << '\t';
-
cout << "Attempts: " << ft.attempts << '\t';
-
cout << "Percent: " << ft.percent << '\n';
-
}
-
-
//计算百分比
-
void set_pc(free_throws & ft)
-
{
-
if (ft.attempts != 0)
-
{
-
ft.percent = 100.0f * float(ft.made) / float(ft.attempts);
-
}
-
else
-
{
-
ft.percent = 0;
-
}
-
}
-
-
//注意这里返回的是值传递 free_throws
-
free_throws accumulate(free_throws & target, const free_throws & source)
-
{
-
target.attempts += source.attempts;
-
target.made += source.made;
-
set_pc(target);
-
return target;
-
}
程序变形后运行结果
两次运行结果对比
程序示例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)