The const modifier in C + + Note 019:c++ is a true constant

Source: Internet
Author: User

Original notes, reproduced please specify the source!

Click "Focus", attention is also a virtue ~

program One:

We know that the subscript of an array cannot be a variable and must be a definite value. To see the program in the C language:

#define a 10

int main ()

{

The first part

int c=20;

int d=10;

int arr[c+d];

Part II

int const A=20;

int const B=10;

int arr[a+b];

return 0;

}

Of course compile errors, because the array subscript cannot be a variable.

See the same program in C + +. There is no difference in C + +, where an array subscript cannot be a variable. Errors are also compiled.

procedure Two :

Continue to see the program changes in the C language:

#define a 10

int main ()

{

The first part

int c=20;

int d=10;

int arr[c+d];

Part II

int const A=20;

int const B=10;

int arr[a+b];

return 0;

}

Compile error found! The cause of the error is consistent with the change!

In other words: in C language , the const definition of a, B is not a constant! Or a variable ! so called " constant variables "!

See the same program in C + +:

int main ()

{

The first part

int c=20;

int d=10;

int arr[c+d];

Part II

int const A=20;

int const B=10;

int arr[a+b];

return 0;

}

Compile through!!!

Conclusion :

C The const modifier in + + is a true constant , and not C in variables ( Read-only ) . C+ + is determined during const-modified constants compilation.

original notes, reproduced please specify the source!

More wonderful please pay attention to the public number: programming according to Law

The const modifier in C + + Note 019:c++ is a true constant

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.