"Constant" const explanation

Source: Internet
Author: User

When I first came into contact with const, I simply remembered that the variable modified by const was a constant. However, this is far from the case of Const.

I. Primary part

1. Use const as the qualifier

(1) Modify common variables and define the const object

const int a = 10;a = 15; //errorconst int b;//errorb = a;

Const limits that the variable a modified by it can only be read but cannot be modified, which is checked during compilation. Variable A is essentially a variable, or a left value, and has an attribute that must be set to the left value.

When const modifies variable B, Initialization is required during definition; otherwise, an error is returned.

In http://www.embedu.org/column/column311.htm, the following code is available:

int main(){   int buf[4];   const int a = 0;   buf[4] = 97;   printf("%d",a);   return 0; }

But what I got in vs2008 is still 0!

(2) The const object is a local variable by default in the file.

First look at the following code:

//--------file_1.cppconst int bufsize = 1234;//--------file_2.cppextern const int bufsize;int main(){    printf("%d",bufsize);    return 0;}

The code above cannot be compiled, because the variable bufsize declared in the global scope is a local variable in the file defining it and cannot be accessed by other files.

Note: When a non-const variable is defined in the global scope, the default value is extern. To enable the const object to be accessed by other files, it must be explicitly specified as extern.

2. Const and define

In Objective C ++ Clause 2: replace # define with const, Enum, and inline whenever possible.

Why?

# Define is a kind of preprocessing. It replaces a character before compilation. It lacks a type check, so it may bring unexpected risks. For more information, see # define. In the programming process, use const instead of # define.

For more information, see Baidu or Google: Folding constants.

3. Const and &,*

A const reference is a reference to a const object. Once a reference is defined, it cannot point to other objects, so it must be initialized. A non-const reference can only be an object of the same type as a non-const object, while a const reference can be initialized to an object of different types or a right value. For example:

int i = 20;double dval = 1.34;const int &r = 20;//okconst int &r2 = i;//okconst int &r3 = i+20;//okconst int &r4 = dval; //ok

For

const int &r4 = dval;

The compiler converts the above Code into the following code:

int temp = dval;const int &r4 = temp;

Next, let's take a look at the const Qualifier and *. Follow the Convention to read the code first:

const int *pContents;//①int * const pContents;//②const int * const pContents;//③

For the above three cases, if you can make it clear, congratulations, you don't have to worry about the const pointer. There is a simple discriminant method in Objective C ++: If the const is on the left of the asterisk, it indicates that the pointer points to a const object, and you cannot modify the value it points, however, you can modify the pointer itself. If the const is on the right of the asterisk, it indicates that the pointer itself is const. You can modify the object to which the pointer points, but the pointer is read-only. ③ Indicates that you cannot modify the pointer or the object pointed to by the pointer.

As mentioned above, for a const object, its reference should have the const feature. Similarly, the pointer to a const object should also have the const feature. If the pointer is non-const and tries to point to a const object, an error is returned. However, the const pointer can accept the address of a non-const object. However, you still cannot use the pointer to modify the value it points to, but you can use other methods to modify the value it points.

Const double Pi = 3.14; double * PTR = π // errordouble * cptr = π // okdouble dval = 2.34; const double * cptr = & dval; // OK * cptr = 3.45; // error // use the non-const pointer to modify the non-const object double * PTR = & dval; * PTR = 3.45; // OK

In C ++ primer, it may be helpful to understand if the pointer to const is understood as "self-thinking pointer to const. Indeed !!!

Ii. Intermediate

1 const and functions

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.