有段程序看不懂,求助

fengmeng_elite 2011-07-09 07:56:32
有个程序看不懂,向高手请教一下(抄自《c++大学教程第二版》):

//create a structure, set its members, and print it.
#include <iostream>

struct Time
{ // structure definition
int hour;
int minute;
int second;
};

void printMilitary ( const Time &);//prototype
void printStandard( const Time &);//prototype

int main()
{
Time dinnerTime; //variable of new type Time

// set members to valid values
dinnerTime.hour=18;
dinnerTime.minute=30;
dinnerTime.second=0;

cout<< "Dinner will be held at ";
printMilitary(dinnerTime);
cout<< "military time,\nwhich is ";
printStandard(dinnerTime);
cout<<" standard time.\n";

// set members to invalid values
dinnerTime.hour=29;
dinnerTime.minute=73;

cout<< "\nTime with invalid values: ";
printMilitary(dinnerTime);
cout<<endl;
return 0;
}
// print the time in military format
void printMilitary( const Time &t)
{
cout<<(t.hour<10? "0" : "")<< t.hour<<":"
<<(t.minute<19? "0" : "")<< t.minute;
}
//print the time in standard format
void printStandard(const Time &t )
{
cout<< ( ( t.hour==0|| t.hour==12)?12: t.hour%12)
<<":"<< (t.minute<10 ? "0": "")<<t.minute
<<":"<< (t.second<10 ? "0": "")<<t.second
<<( t.hour<12 ? "AM" : "PM");
}

-----------------------------------------------------------

其中看不懂的是:

void printMilitary( const Time &t)

1."&t"如何引用的了dinnerTime的数据

2.&t代表的是一串地址,为什么后面“(t.minute<10 ? "0": "")”反而可一旦数据用,

这里面没有定义指针啊?


...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aa0758 2011-07-09
  • 打赏
  • 举报
回复
引用就是指向地址
调用函数时将操作同一个数据,函数可以修改传递的参数

除了引用类型,还有一个值类型,调用函数时只是值拷贝
jixingzhong 2011-07-09
  • 打赏
  • 举报
回复
关于引用:

http://baike.baidu.com/view/2129184.html?fromTaglist
jixingzhong 2011-07-09
  • 打赏
  • 举报
回复
void printMilitary( const Time &t)

其中的 & 不是表示取地址,
而是表明参数是一个引用。

引用,只是别名,
也就是说,形参和实参,两个名字,其实是同一个变量。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧