This article refers to the C + + programming thought eighth chapter content, read this book, please disregard the following content.
In C + +, Const has a total of four functions, respectively, for value substitution, pointers, function parameters and returns, and classes.
One value substitution
The original motive of 1.const is value substitution, that is, the typical use of value substitution in the C language previously was this:
#define BUFSIZE 100
You can now use:
const int bufsize = 100;
The benefit of this substitution is that type checking can be done to avoid some problems.
2. The value of the const defined in this way can be determined during compilation, so it can be used to define an array, such as:
Char Buf[bufsize];
The 3.const variable must be initialized when it is defined. This should be the case:
const int a = 100;
If just declare:
const int A;
will give an error, do not believe you try?
4. The reason is that the const variable in C + + defaults to an internal connection, that is, in the file where the variable is valid, equivalent to the static effect, so it is not a problem to define a const variable with the same name in different files. But what if multiple files are to share the same const variable? Define and initialize in one place, like this:
extern cons Tintx = 1;
and declare in other documents:
extern constint x;
Therefore, the reason that the const variable is initialized when it is defined is that it distinguishes the definition from the declaration.
Two pointers
1. Pointer to const
const INT * U;int const * U;
Both of these mean: U is an int pointer pointing to a constint. That is, a pointer to a constant.
2.const pointer
int d = 1;int * Const W =&d;
W is an int pointer that points to an int value and the W pointer itself does not change.
Ps: Why there are two statements, with reference to the above, the const variable definition needs to be initialized.
3. In combination with 1th and 2nd, you can define a const quantity that does not change the pointer and the value that the pointer points to, how do you guess?
4. You can assign an address of a non-const variable to a const pointer, but you cannot assign the address of a const value to a non-const pointer.
int d = 1;const int a = 1;const int * b =&d; Rightint * C =&a; Wrong
Three function parameters and return types
1.const variable is passed by value, the value cannot be changed.
void F1 (Constint i) { i++;//wrong}
When 2.const is a return value, it doesn't make sense. Because you can't get the effect you want, you can try it.
3.const pointer as a function parameter case. This is a complicated situation, just remember that for a user-defined data type, you must add a const when using a reference of that type as a function parameter.
Four categories
Member variables of type 1.const and their initialization
Class Integer { int i;public: integer (int ii = 0); void print ();};i Nteger::integer (int II = 0): I (ii) {}void Integer::p rint () {cout << i << ';}
2. Constants in the class during compilation. Must be initialized at the time of definition.
Class StringStack { static const int size = +; Const string* Stack[size]; int index;public: stringstack (); void push (const string* s); Const string* pop ();}; Stringstack::stringstack (): Index (0) { memset (stack,0,size * sizeof (String *));} void StringStack::p ush (const string* s) { if (Index < size) stack[index++] = s;} Const string* StringStack::p op () { if (Index > 0) { const string* RV = Stack[--index]; Stack[index] = 0; return RV; } return 0;}
The concept of 3.const objects is that the data members of an object are not changed during their life cycle. Therefore, it can only call the const member function and cannot call a non-const member function, because it is possible to change the data member.
Class Quoter { int lastquote;public: quoter (); int lastquote () const;}; Quoter::quoter () { lastquote =-1; Srand (Time (0));} int Quoter::lastquote () const { return lastquote;}
Const usage in C + +