📄 demo_5_const_reference_2.cpp
字号:
//************************************************
// 常引用: const 类型说明符 &引用名
// 常引用做形参不会发生对实参意外的更改。
//************************************************
# include <iostream.h>
class Time
{
public:
Time(int,int,int); //构造函数的声明
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s) //构造函数的实现
{
hour=h;
minute=m;
sec=s;
}
//对象的常引用做形参,在函数中不能更新所引用的对象,因此对应的实参不会被破坏。
void fun(const Time &t)
{
// t.hour=18; //Error: 对象的常引用的数据成员不能作为左值被修改
cout<<t.hour<<endl;
}
int main()
{
Time t(10,13,56);
fun(t);
cout<<t.hour<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -