#include
using namespace std;
class Time
{
public:
Time(int h,int m,int s):hour(h),minute(m),sec(s){}
int hour;
int minute;
int sec;
};
void fun(Time &)
{t.hour=18;}
int main()
{
Time t1(10,13,56);
fun(t1);
cout<<t1.hour<<endl;
return 0;
}
此时fun函数形参是是实参的引用,t1.hour可以被修改。如果把fun函数改为void fun(const Time &t),此时函数不能修改t1的值。
我想问 fun函数写成void fun(Time t),同样可以做到函数不能修改t1的值,那这个常引用存在的意义是什么?或者请哪位大神可以举一个常引用应用的例子
对象常引用的问题c++语言
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-