c++
class Screen {
public:
void home() { _cursor = 0; }
char get() { return _screen[_cursor]; }
};
void Demo::nonInline() { std::cout « “nonInline() called” « std::endl; }
void Demo::inlineViaDecl() { std::cout « “inlineViaDecl() called” « std::endl; }
inline void Demo::inlineViaDef() { std::cout « “inlineViaDef() called” « std::endl; }
inline void Demo::inlineBoth() { std::cout « “inlineBoth() called” « std::endl; } ```
const and volatile member functionsclass Screen {
public:
bool isEqual( char ch ) const;
private:
string::size_type _cursor;
string _screen;
};
bool Screen::isEqual( char ch ) const
{
return ch == _screen[_cursor];
}
#include <iostream>
class Screen {
public:
void get(int x, int y) {
std::cout << "Called non-const member" << std::endl;
};
void get(int x, int y) const {
std::cout << "Called const member" << std::endl;
};
};
int main() {
const Screen cs;
cs.get(0,0); // calls const member
Screen s;
s.get(0,0); // calls non-const member
}
To allow a class data member to be modified even though it is the data member of a const object, we can declare the data member as mutable. A mutable data member is a member that is never const, even when it is the data member of a const object. A mutable member can always be updated, even in a const member function. To declare a member as a mutable data member, the keyword mutable must precede the declaration of the data member in the class member list.