您的位置:首页 > 编程语言 > C语言/C++

C++ 一些小的知识点

2017-07-10 09:55 253 查看
很久不接触C++了,工作需要,很多东西都忘记了,只好把一些细小的知识点记录在这里帮助自己记忆。

Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.

从上面的说明来看,constant member function 有两个方面,一是它不能改变对象的非const数据成员,也不能调用对象的非const方法。二是,它可以改变对象的const数据成员[注意const数据成员本来是不可以改变的],可以调用对象的const方法。

 

1、const放在最后,表示此成员函数不改变类中的成员变量。

2、const放在函数参数中,表示此参数在函数体不会被改变。

3、const放在最前,表示返回const值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: