Click here to Skip to main content
Click here to Skip to main content

Const Illustration in C++

By , 17 Sep 2008
Rate this:
Please Sign up or sign in to vote.

Introduction 

A const object is an object that should not be modified. Here's the possible usage:

//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. The following examples cover the other const usages.

Using the Code 

Const in Header

//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  

//file header.h
class CX
{
public:
    CX();
private:
    const int member_const_count;
    static const int static_member_const_count = 7;
};

//file source.cpp
#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

//file header.h
class CX
{
public:
    CX();
    void increase();
    int getCount() const;
private:
    int member_ count;
};

//file source.cpp
#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;
    //cx.increase(); //Error, cx can't be changed.
    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:

//file header.h
class CX
{
public:
    CX();
    void increase();
    int getCount() const;
private:
    mutable int cache_data;
    int member_count;
};

//file source.cpp
#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”.

//file header.h
class CX
{
public:
    CX();
    void increase();
    int getCount() const;
private:
    mutable int cache_data;
    int member_count;
};

//file source.cpp
#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);
    //*pi = 9;//compile OK, runtime error. Change constant ci

    const CX cx;
    CX* px = const_cast<CX*>(&cx);
    px->increase(); //The change won't be noticed because cx is excepted as const
    std::cout << cx.getCount() << std::endl;

    char* sz = "string"; //Compiler do casting
    //sz[0] = 'c'; //runtime error

    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Comments and Discussions

 
GeneralMy vote of 5 PinmemberDinoLL++4-Mar-13 8:05 
GeneralThis is all correct Pinmemberjuggler19-Sep-08 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 17 Sep 2008
Article Copyright 2008 by LaymanZheng
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid