C + + 's const

Source: Internet
Author: User

In the C, C + +, and D programming languages, Const is a type qualifier, a keyword applied to a data type that indicates tha t the data is constant (does not vary). While this can is used to declare constants, const in the C family of languages differs from similar constructs in other L Anguages in being part of the type, and thus have complicated behavior when combined with pointers, references, composite D ATA types, and type-checking.

Const was introduced by Bjarne Stroustrup in C with Classes, the predecessor to C + +, in 1981, and was originally called RE Adonly. As to motivation, Stroustrup writes:

"It served, functions:as A," Defining a symbolic constant that obeys scope and type rules (which is, without using A macro) and as a the deeming an object in memory immutable. "

The first use, as a scoped and typed alternative to macros, is analogously fulfilled for Function-like macros via the INL ine keyword. Constant pointers, and the * const notation, were suggested by Dennis Ritchie and so adopted.

Const was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with the other Type qualifier, volatile. A further qualifier, Noalias, was suggested in the December 1987 meeting of the X3J11 committee, but was rejected; Its goal is ultimately fulfilled by the Restrict keyword in C99. Ritchie was wasn't very supportive of these additions, arguing that they didn't "carry their weight", but ultimately did not Argue for their removal from the standard.

D subsequently inherited const from C + +, where it is known as a type constructor (not type qualifier) and added R type constructors, immutable and inout, to handle related use cases.

#include <iostream> #include <stdlib.h>using namespace std;void fun (const int &a, const int &b), int ma In (void) {int x = 3;int y = 5;fun (x, y), cout << x << "," << y << endl;system ("pause"); return 0;} void Fun (const int &a, const int &b) {a = 10;b = 20;}

The above procedures run the times wrong, because the Const keyword is qualified, so that the function parameters can not change the original value of the argument, so that in other program design to avoid the occurrence of misoperation. In this example, the value of A/b cannot be changed.

C + + 's const

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.