Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 7:52 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Const illustration in C++
PostPosted: Sat Nov 08, 2008 11:32 pm 
Offline
Expert
User avatar

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;
};

//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 we define it.

* Const class member function


Code:
//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.
Code:

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

//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, runtion error. Change constant ci

    const CX cx;
    CX* px = const_cast<CX*>(&cx);
    px->increase(); //The change won’t be niticed 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:

Code:
char sz[] = "string";


Further reading

Think in C++ 2ed. Vol1, Chapter 8. Constants. by Bruce Eckel

Effective C++ 3rd. Item 3: Use const whenever possible. by Scott Meyers

 Article by:   LaymanZheng

_________________
Any help needed just reply to my topic ,
ccna ,ccnp certified .


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team