Constant Member Functions
来源:互联网 发布:数据库工程师待遇 编辑:程序博客网 时间:2024/05/20 13:13
Constant Member Functions
Visual Studio 2013
Other Versions
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. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.
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.
// constant_member_function.cppclass Date{public: Date( int mn, int dy, int yr ); int getMonth() const; // A read-only function void setMonth( int mn ); // A write function; can't be constprivate: int month;};int Date::getMonth() const{ return month; // Doesn't modify anything}void Date::setMonth( int mn ){ month = mn; // Modifies data member}int main(){ Date MyDate( 7, 4, 1998 ); const Date BirthDate( 1, 18, 1953 ); MyDate.setMonth( 4 ); // Okay BirthDate.getMonth(); // Okay BirthDate.setMonth( 4 ); // C2662 Error}
Reference
const (C++)
Constant Values
0 0
- Constant Member Functions
- Constant Member Functions
- Constant Member Functions
- Constant Member Functions
- const (Constant) Objects and const Member Functions
- Static member functions in C++
- c++: Difference between member and non member functions
- How Non-Member Functions Improve Encapsulation ZZ
- Chapter 5 Data Hiding and Member Functions
- Window Procedures as Class Member Functions
- 让 lua 调用 C++ 中的 member functions
- const member functions and const return values
- C++之虚函数(Virtual Member Functions)
- Item16 Make const member functions thread safe
- 《深度探索C++对象模型》:member functions
- Effective C++ 23. Prefer non-member non-friend functions to member functions
- 关于C++ 的constant Member function 的讲解
- 混沌 IN C++::Pointers-to-Member functions 解迷
- 源码推荐(1月6日):3D效果的界面转场 把刷新变成游戏的控件
- Java实现两个整数相除
- 努力吧,现在也不晚
- 利用animation-list做每隔几秒显示一张图片的动画效果
- 西川善司的3D图形技术概念和渲染管线的处理
- Constant Member Functions
- Android Surfaceflinger源码分析
- 判断iOS设备类型
- C++回顾之static用法总结、对象的存储,作用域与生存期
- 编译器错误 C2662
- 代码库开源项目推荐(1.3)
- Linux下设置Oracle开机自启动
- adnroid 短信 发送
- Qt中QTableWidget用法总结