Thursday, October 22, 2009

Const in a Nutshell

That heading sure could be misconstrued. Anyway, here are the rules of const:

1. Type const is used for unmodifiable objects.
2. Attempts to modify const objects will cause execution errors.
3. Member functions cannot call const objects unless the function is const.
4. Member functions that modify objects cannot be const.
5. Const member functions cannot call non-const object in the same class instance.
6. You can't use a non-const member function to call a const object.
7. One solution is to overload the const member function with a non-const version.
8. Constructors or destructors can never be const because they change objects.
9. Constructors may still be used to initialize const objects.
10. Const data members and data members referenced by const MUST be initialized in using member initializers.

No comments:

Post a Comment