Mutable - Mutable In C+ it's possible to have a class...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Mutable In C++ it's possible to have a class object instance that is constant and cannot be modified by the program, once initially set up. For example: class A { public: int x; A(); }; const A a; void g() { a.x = 37; } is illegal. In a similar way, invoking a non-const member function on a const object is also illegal: class A { public: int x; A(); void f(); }; const A a; void g() { a.f(); } The reason for this latter prohibition is due to separate compilation. A::f() may be defined in some other translation unit, and there's no way of knowing whether it modifies the object upon which it operates. It is possible to define const member functions: void f() const; that are allowed to operate on a const object instance. Such a function does not modify the instance it operates on. The type of the "this" pointer for a class T is normally: T *const this; meaning that the pointer cannot be changed. Within a const member function, the type is: const T *const this; meaning that neither the pointer nor the pointed-at object instance can be modified. Recently a new feature has been added to C++ to selectively allow for individual data
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

Mutable - Mutable In C+ it's possible to have a class...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online