这里使用const的用法是什么 [英] What's the use of const here

查看:122
本文介绍了这里使用const的用法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

INT工资()const的{返回mySalary; }

据我了解常量是这个指针,但我不知道。任何一个可以告诉我什么是在这里使用const?

as far as I understand const is for this pointer, but I'm not sure. Can any one tell me what is the use of const over here?

推荐答案

听起来像是你有正确的想法, 。在一个对象的方法的C ++常量意味着该方法不能修改该对象

Sounds like you've got the right idea, in C++ const on a method of an object means that the method cannot modify the object.

例如,这将不会被允许

class Animal {
   int _state = 0;

   void changeState() const { 
     _state = 1;
   }
}

这篇关于这里使用const的用法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆