Introduction
A const
object is an object that should not be modified. Here's the possible usage:
const int global_const_count = 7;
class CX
{
public:
CX();
const int foo(const int) const; private:
const int member_const_count; static const int static_member_const_count = 7; };
#include "header.h"
const int CX::foo(const int) const {
const int i = 1; return i;
}
int main()
{
return 0;
}
Const
parameter, const
return value and local const
are nothing special but with the semantic constraint. The following examples cover the other const
usages.
Using the Code
Const in Header
static int global_count;
const int global_const_count = 7;
#include "header.h"
int main()
{
return 0;
}
#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
class CX
{
public:
CX();
private:
const int member_const_count;
static const int static_member_const_count = 7;
};
#include "header.h"
CX::CX()
: member_const_count(7)
{
}
int main()
{
return 0;
}
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 where we define it.
Const Class Member Function
class CX
{
public:
CX();
void increase();
int getCount() const;
private:
int member_ count;
};
#include <iostream>
#include "header.h"
CX::CX()
: member_count(0)
{
}
void CX::increase()
{
++member_count;
}
int CX::getCount() const
{
return member_count;
}
int main()
{
const CX cx;
std::cout << cx.getCount() << std::endl;
return 0;
}
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:
class CX
{
public:
CX();
void increase();
int getCount() const;
private:
mutable int cache_data;
int member_count;
};
#include <iostream>
#include "header.h"
CX::CX()
: cache_data(0)
, member_count(0)
{
}
void CX::increase()
{
++member_count;
}
int CX::getCount() const
{
++cache_data;
return member_count;
}
int main()
{
const CX cx;
std::cout << cx.getCount() << std::endl;
return 0;
}
Cache_data
is changed when we 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”.
class CX
{
public:
CX();
void increase();
int getCount() const;
private:
mutable int cache_data;
int member_count;
};
#include <iostream>
#include "header.h"
CX::CX()
: cache_data(0)
, member_count(0)
{
}
void CX::increase()
{
++member_count;
}
int CX::getCount() const
{
++cache_data;
return member_count;
}
const int ci = 10;
int main()
{
int* pi = const_cast<int*>(&ci);
const CX cx;
CX* px = const_cast<CX*>(&cx);
px->increase(); std::cout << cx.getCount() << std::endl;
char* sz = "string";
return 0;
}
To modify the string
, put it in an array first:
char sz[] = "string";
Further Reading
- Think in C++ 2ed. Vol 1, Chapter 8. Constants. by Bruce Eckel
- Effective C++ 3rd. Item 3: Use const whenever possible by Scott Meyers
History
- 9/17/2008 Initial creation of article