C + + constant const

Source: Internet
Author: User
Tags array definition

Constant folding Concept

The effect on a constant folding surface is the same as the macro substitution, except that "the effect is the same", and the real difference between the two is that the macro is a character constant, after the precompiled macro substitution is complete, the macro name disappears, all references to the macro have been replaced with its corresponding value, the compiler certainly does not need to maintain the symbol, When a constant collapse occurs, the reference to the constant is replaced with the value of the constant, but the constant name does not disappear, and the compiler places it in the symbol table, allocating space, stack space, or global space for the variable.

voidconst_test () {intI0 = One; Const intI=0;//Defining Constants I    int*j = (int*) &i;//See here can take the value of I, Judge I must after their own memory space*j=1;//Modify the memory that J points toprintf"%d\n%d\n%d\n%d\n", &i,j,i,*j);//Watch the experimental results    Const intCK =9;//This control experiment is to observe that the effect of a reference to a constant CK    intIK =ck; intI1 =5;//This control experiment is to distinguish between constants and references to variables.    intI2 =I1; }

Output Result:

0012ff7c

0012ff7c

0

1

(1) The IJ address is the same, *j==1, and the value of I is actually equal to 0
(2) printf (" %d\n%d\n%d\n%d\n", &i,j,i,*j) is not replaced by printf ( "%d\n%d\n%d\n%d\n", &i,j,0,*j)
(3) A reference to a collapsible constant is replaced with the value of the constant, and the reference to the variable requires access to the memory of the variable
The const error in the collection is used

The const can be used for collections, but the compiler cannot store a collection in its symbol table, so memory must be allocated. In this case, const means "a piece of memory that cannot be changed." However, its value cannot be used at compile time because the compiler does not need to know what is stored at compile time.

const int i[] = {1,2,3,4,5};
int a = i[3];
float F[i[3]]; Illegal

In an array definition, the compiler must be able to produce such a stack pointer code for a moving storage array, in the illegal definition above, the compiler gives the hint because it cannot find a constant expression in the array definition.

The difference between const in C and C + +
Const int Ten ; Char buff[buffsize];

The above code is possible in C + +, but not in C because Buffsize occupies somewhere in the store, so the C compiler does not specify its value at compile time

Const int buffsize;

The above code can be in C, because the default const in C is an external link, C + + default const is internal link, so in C + + to be written as extern const int buffsize;

Const and Enum in class

In a class, Const restores part of its meaning in C, which allocates storage in each class object and represents a value that cannot be changed once initialized. The use of const in a class means "this is a constant over this object's life cycle." However, for this constant, each different object can contain a different value . So it can't be written like this:

class bob{    constint;     int array[nsize];};

 Because the class object is allocated for storage, the compiler does not know what the const content is, so it cannot be used as a constant during compilation.

We can use an unmarked enum without an instance

class bob{   enum, n}        ; int array[nsize];};
using an enum does not consume storage space in an object, and enumeration constants are evaluated at compile time. Use 4 bytes of space when enumerating outside the class

thus, when a const is created in a class, it cannot be given an initial value. This initialization must take place in the constructor, and in a special place in the constructor function. because the const must be initialized in the place where it was created, all in the body of the constructor, the const must have been initialized, otherwise it would have to wait until it is initialized after the constructor body, which prevents the const value from being changed in different places in the constructor body.

class fred{private:    constint  nSize;  Public :    Fred ();}; Fred::fred (): NSize (+) {}

C + + constant const

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.