Constant Member Functions
来源:互联网 发布:新世纪网络平台 编辑:程序博客网 时间:2024/05/19 10:42
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.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}
- 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 解迷
- servlet 使用spring管理bean
- 2013年10个稳赚行业曝光
- 简单servlet socket 整合
- 设置Weblogic以提高并发处理性能
- stringcat再会
- Constant Member Functions
- java生成xml文件
- Android圆角和边框的实现
- android展讯6820内置可卸载可恢复的apk应用
- Android — get MEID
- JQuery方法大全
- MFC EnumChildWindows的使用
- json数据解析
- EJB3.0 无状态会话bean的编写,及客户端调用