Const variable in C ++)

Source: Internet
Author: User

1:

Only some text is replaced during preprocessing, And the type check function is not available.
# Define bufsize 100
Bufsize is a name that exists only during the pipeline processing. Therefore, it does not occupy storage space and can be placed in a header file, the purpose is to provide a value for all the compilation units that use it.
C ++ uses const to replace the value with the compiler. You need to know where the value is stored using bufsize and can perform constant folding;
Const int bufsize = 100;
Char Buf [bufsize];
Similarly, you must put the const definition in the header file. by including the header file, you can put the const definition separately in one place and allocate it to a compilation unit. const in C ++ is an internal connection by default. const is visible only in the file defined by const, and cannot be seen by other compilation units during connection. When defining a const, a value must be assigned to it unless it is explicitly described using extern:

Extern const int bufsize;

 

2:

The C ++ compiler does not create a bucket for const. Instead, it stores the definition in its symbol table. However, the extern above forcibly allocates the bucket, (If a const address is obtained, the bucket must be allocated.) When extern is not a part of the definition, no bucket is allocated.

 

3:

Const can be used for collections, but does not store a set in its symbol table. Therefore, memory must be allocated. Const means that a bucket cannot be changed. However, you cannot use its value during compilation because the compiler does not need to know the stored content during compilation.

 

4:

The literal value of a character array is created by the compiler as a constant character array. The result of referencing this character array is its first address in memory. Modifying any character in the character array will cause a runtime error;

 

5:

When processing user-defined types, return Constants by value is very important. If a function returns a class object as const by value, the return value of this function cannot be a left value (that is, it cannot be assigned or modified)

 

6:

The compiler automatically generates all temporary values as const variables. It is possible to pass a temporary object to a function that accepts const references, but cannot pass a temporary object to a function that accepts pointers, for a pointer, it must explicitly accept the address.

 

7:

The const member function can only be called for a const object. In a class, const allocates storage in each class object and represents a value. Once initialized, this value cannot be changed. Using const in a class means "it is a constant in the lifetime of this object". For this constant, each different object can contain a different value.

When a common const is created in a class, the initial value cannot be given. This initialization must be performed in the constructor.

 

 

8:

It must be initialized where static const is defined;

Two common types:

Static const size = 1000;

Enum {size = 1000 ;}

 

9:

The compiler forces the programmer to reiterate the const description when defining a function (const has become part of the Function Identifier, so both the compiler and the Connection Program must check Const.

 

10:

It is safe for a const member function to call const and non-const objects;

Const and destructor are not const member functions, because they always modify objects during initialization and cleanup. Use the keyword mutable in the class declaration to specify that a specific data member can be changed in a const object. If an object is defined as a const object, he becomes a candidate to be put into read-only memory;

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.