Joined: Tue Nov 06, 2007 2:17 pm Posts: 847 Has thanked: 0 time Have thanks: 1 time
Introduction
A const object is is an object that should not be modified. Here's the possible usage.
Code:
//file header.h const int global_const_count = 7; // global const in header file
class CX { public: CX(); const int foo(const int) const; //const member function with a const parameter and return a const private: const int member_const_count; //non-static const member static const int static_member_const_count = 7; //static const member };
//file source.cpp #include "header.h" const int CX::foo(const int) const //const member function implementation { const int i = 1; //local const return i; } int main() { return 0; }
Const parameter, const return value and local const are nothing special but with the semantic constraint. Following examples cover the rest const usages. Using the code
1. Const in header
Code:
//file header.h static int global_count; const int global_const_count = 7;
//file source1.cpp #include "header.h"
int main() { return 0; }
//file source2.cpp #include "header.h"
As we can see both source1.cpp and source2.cpp include the definition of global_const_count, a const in C++ must default to internal linkage. That is, it is visible only within the file where it is defined and cannot be seen at link time by other translation units. But there won’t be two instances of the const. Normally, the C++ compiler avoids creating storage for a const, but instead holds the definition in its symbol table.
* Const class member data
Code:
//file header.h class CX { public: CX(); private: const int member_const_count; static const int static_member_const_count = 7; };
A non-static const member is a constant for the lifetime of the object. As a constant must be initialized when it is created, the const member must be initialized in the class member initializer list. But for a static const member, it belongs to the class not a certain instance. So we can’t initialize it as non-static const member does. Thus, we have to do initialization at the point we define it.
* Const class member function
Code:
//file header.h class CX { public: CX(); void increase(); int getCount() const; private: int member_ count; };
A const member function guarantees that it won’t modify the object instance. Thus, it’s legal to invoke the const member function on a const object, while it’s not for a normal member function. There’s no static const member function. A static member function doesn’t belong to any object, so there’s no object to change.
* Mutable
If we want to change a data member of a const object for some reason, for example the cache data, we can use keyword mutable as the following example.
Code:
//file header.h class CX { public: CX(); void increase(); int getCount() const; private: mutable int cache_data; int member_count; };
Cache_data is changed when invoke int CX::getCount() const. But it’s transparent to the user, the object is “logical constâ€.
* Const_cast
We can use const_cast to remove const of an object. But doing this will bring trouble in most cases. Interestingly, the C++ compiler will do the cast in character array for “historic reasonâ€.
Code:
//file header.h class CX { public: CX(); void increase(); int getCount() const; private: mutable int cache_data; int member_count; };