使用结构引用参数的方式与使用基本变量引用相同,只需在声明结构参数时使用引用运算符&即可。
假设有如下结构定义:
struct free_throws
{
std::string name;
int made;
int attemp;
float percent;
}
则可以这样编写函数原型,在函数中将指向该结构的引用作为参数:
void set_pc(free_throws & ft);
如果不希望函数修改传入的结构,可使用const:
void set_pc(const free_throws);
#include<iostream>
#include<string>
using namespace std;
void display(const free_throws& ft);
void set_pc(free_throws& ft);
free_throws& accumulate(free_throws& target, const free_throws& source);
struct free_throws
{
string name;
int made;
int attemp;
float percent;
};
int main()
{
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 = { &#