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.cpp class 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 const private: int month; }; int Date::getMonth() const { return month; // Doesn't modify anything }
No comments:
Post a Comment