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