close
class student
{
private:
int roll;
public:
// constructor
student(int r):roll(r) {}
// A const function that changes roll with the help of const_cast
void fun() const {
(const_cast <student*> (this) )->roll = 5;
}
int getRoll() {
return roll;
}
};
int main(void) {
student s(3);
cout << "Old roll number: " << s.getRoll() << endl;
s.fun();
cout << "New roll number: " << s.getRoll() << endl;
return 0;
}
Old roll number: 3
New roll number: 5
from :
http://www.geeksforgeeks.org/casting-operators-in-c-set-1-const_cast/
全站熱搜
留言列表