C++ const keyword summary

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A lot about the knowledge of const C++, do a summary here.

The const keyword modified variables in the created value cannot be changed, therefore must be initialized when it is created.

The type of an object determines which can be operated on the object. The const object, can only use the operation can not change the state of the object.

If the const object is a compile time constant (compile-time constant) to initialize:

const int bufSize = 512; // input buffer size 

Then the compiler will replace the const variables with the constants at compile time. The compiler to replace all use bufSize place 512. In order to carry out this substitution, the compiler needs to know what is apparently the initial value of const variables, it needs to check the const variable is to be defined and initialized.

If the program distribution in multiple files, in order to replace the value of the variable, the compiler uses the const variable for each file, all need to know the const variable is to be defined, which requires the use of the const variable in each file are defined on the variable (define instead of declare), but a variable cannot be repeated definition. In order to solve this contradiction, regulation scope const variable is the document itself(local to file). When multiple const variables we defined in multiple files of the same name, like in each file defines a different variable.

(For general variables can only be defined in one place, to use in other documents need to use the extern keyword to declare. )

Sometimes we want to share a const variable in multiple files, but the const variable initializer (initializer) is not a constant expression (constant expression, the value of the constant expression can be determined at compile time, such as a number or an arithmetic expression, an initialized by constant expression const variable is a constant expression). In this case, we do not want the compiler uses the const variable in each file individually to generate a new variable. We hope that is defined in one file, declared in the other documents. To achieve this goal, needs in the definition and declaration " and using the extern keyword (or local to file).

// file_1.cc 
extern const int bufSize = fcn(); 
 // file_1.h 
extern const int bufSize; 

The following code specific demonstration. If the nonconst variable, can only be defined in a.

// file_1.cc 
#include <cstdio> 
int bufSize = 512; 
void print(); 
int main() { print(); return 0; } 
// file_2.cc 
#include <cstdio> 
int bufSize = 100; 
int print() { printf("\n%d\n", bufSize); return 0; } 

Compile times.: multiple definition of bufSize'. If one of two bufSize to const, or two to const, the compiler through, the output of 100.

// file_1.cc 
#include <cstdio> 
const int bufSize = 512; 
void print(); 
int main() { print(); return 0; } 
// file_2.cc 
#include <cstdio> 
extern const int bufSize; 
int print() { printf("\n%d\n", bufSize); return 0; } 

If so, do not include the extern keyword in file_1, then compile times.: undefined reference to bufSize'.

If the const keyword two files are removed, or to the file_1 const variable in the extern keyword, also compiled successfully, the output of 512.

A reference to the const variable.

Like ordinary variables, but also can be a reference is bound to a const variable. On the const variable to reference the same can not change the value of a const variable.

const int ci = 1024; 
const int &r1 = ci; r1 = 42; // Error, cannot pass a reference to the const changes the value of the const variable 
int &r2 = ci; // Error, cannot use the nonconst reference reference to the const variable 

The pointer, const and type the alias(type aliases)

Consider the following code:

typedef char *pstring; const pstring cstr = 0; const pstring *ps; 

Const modified pstring, and the type of pstring is a pointer to char, so const pstring is a constant pointer to char, rather than pointer to const char.

Not by direct substitution to understand a typedef:

const char *cstr = 0; // This is the wrong understanding of const pstring CSTR 

The const parameter

Assignment, top-level const will be ignored, so the following two function definition does not exist:

void fcn(const int i) {} 
void fcn(int i) {} 

These two functions can receive the same parameters, so not too heavy.

You can initialize the low-level const object using the nonconst object, but not with the low-level const object to initialize the nonconst object. (low-level const pointer that points to the object is a const object).

int i = 42; 
const int *cp = &i; // Ok: CP is low-level const, can be initialized with nonconst, but cannot change the value of I by CP 
const int &r = i; // ok: reference to const, Cannot change the value of I by R 
const int &r2 = 42; // ok 
int *p = cp; // Error: cannot use low-level const to initialize the nonconst, but can be coerced by const_cast 
int &r3 = r; // error 
int &r4 = 42; // Error: cannot initialize plain by literal reference 

If possible, you should use the reference to const

Don't reference to const, will let the caller mistakenly believe that the function may change the values of the parameters. And you don't have to reference to const, parameter type constraints can transfer. Can't pass a const object to the ordinary reference, can not transfer literal, can not transfer types need to be converted.

The const member function

You can add the keyword const member function's parameter list behind, is modified this pointer.

This const pointer to nonconst pointer is object by default, so can't point to const object, is the method of the const object cannot be invoked. The member function to the const member function, the this pointer implicit parameter into const pointer to const object. The const member function does not change the state of the object. Const object can call the const member function.

Note that the const member function returns *this if, then returns an const object, can not continue to member function call nonconst in the returned object basis. In order to solve this point can be a member function of constness based on the member function overloading.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lillian at November 14, 2013 - 12:22 AM