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/

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()