Total members 11894 |It is currently Thu Nov 21, 2024 10:38 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





How to define constant in C++. A const object is is an object that should not be modified. Here's the possible usage.

cpp 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

Const in header:

cpp 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 :


cpp 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:


cpp 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 is legal to invoke the const member function on a const object, while it is not for a normal member function. There is no static const member function. A static member function doesn't belong to any object, so there is 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.
cpp 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 is 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".

cpp 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:

cpp code
char sz[] = "string";




_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 




Topic Tags

C++ Variables
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com