const and its usage in different languages C and C ++,

(1) constant const can be defined.

(2) Const may be modified parameters and return values ​​of the function, even definition body. Modified const things are subject to mandatory protection.

When (3) Const modified member function, for the front member function, the return value can not be left as the value. Used in the back, the data members of the member function can not be changed.

When (4) Const modified data member, you must use a colon syntax.

In the C program, const mainly defined variables, function parameters modified, the modified function return value.

In the C ++ program, it can also be modified definition body function, defined as the class member functions steady state function, not change the data members of the class.

When the array is defined dimension N, and define const can be defined by N:

Such as:

 const int N =10;
 char a[N];
或
#define N 10
 char a[10];

At this time, as the number of N-dimensional array, only as a constant. Int N = 10; char a [N]; is wrong, because the N is variable.

different const with the #define?

(1) const constant data types, no macro constants.

(2) compiler const can type security checks, define the character can only be replaced.

(3) Some integrated debugging tools to debug const constant, but you can not debug the macro constants.

Constant use const only in C ++ programs without using a macro constants .

When (4) const modified member variable, initialize member variables must be initialized in the constructor list (colon syntax) in.

Guess you like

Origin www.cnblogs.com/yuanqiangfei/p/11248125.html