函数的返回值 const

函数都有返回值,即便是返回值为空,也可以看做有void类型的返回值,函数的返回值可以带来函数的处理结果。

函数的返回值有:

  一:值方式

  二:常值方式  const标识符使该对象的私有成员不能被改变

  三:引用方式

  四:常值引用方式

class temperature
{
private:
 float hightemp;
 float lowtemp;
public:
 temperature(float h,float l){hightemp=h;lowtemp=l;};
 void update(float temp);//返回值为值方式
 float gethightemp(void)const;//返回值为常值方式
 float getlowtemp(void)const;//返回值为常值方式

};
void temperature::update(float temp)
{
 if (temp>hightemp)hightemp=temp;//允许改变私有数据
 if (temp<lowtemp)lowtemp=temp;
}
float temperature::gethightemp() const
{
 hightemp=2*hightemp;//出错,私有数据hightemp被改变
 return hightemp;
}
float temperature::getlowtemp()const
{
 return lowtemp;//正确,未改变私有数据
}

posted on 2013-03-27 09:33  城市的孩子在退化  阅读(447)  评论(0)    收藏  举报