c:c_const_declaration
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
c:c_const_declaration [2021/10/04 19:49] – peter | c:c_const_declaration [2023/06/26 07:24] (current) – [Messier Still - in the Object Oriented Programming] peter | ||
---|---|---|---|
Line 7: | Line 7: | ||
However it is also used to bodge in a substitute for one of the missing features of C++ and there it gets horridly complicated and sometimes frustratingly restrictive. | However it is also used to bodge in a substitute for one of the missing features of C++ and there it gets horridly complicated and sometimes frustratingly restrictive. | ||
- | The following attempts to explain how 'const' | + | The following attempts to explain how **const** is used and why it exists. |
---- | ---- | ||
Line 13: | Line 13: | ||
===== Simple Use of ‘const’ ===== | ===== Simple Use of ‘const’ ===== | ||
- | The simplest use is to declare a named constant. This was available in the ancestor of C++, C. | + | Declare |
- | + | ||
- | To do this, one declares | + | |
- | + | ||
- | * One has to initialize it immediately in the constructor because, of course, one cannot set the value later as that would be altering it. For example, | + | |
<code cpp> | <code cpp> | ||
Line 23: | Line 19: | ||
</ | </ | ||
- | will create an integer constant, unimaginatively called ‘Constant1’, with the value 96. | + | <WRAP info> |
+ | **NOTE: | ||
- | Such constants are useful for parameters which are used in the program but do not need to be changed after the program is compiled. | + | * Such constants are useful for parameters which are used in the program but do not need to be changed after the program is compiled. |
+ | * This has to be initialized immediately in the constructor because, of course, one cannot set the value later as that would be altering it. | ||
+ | * Using a constant has an advantage over the C preprocessor **#define** command in that it is understood & used by the compiler itself, not just substituted into the program text by the preprocessor before reaching the main compiler, so error messages are much more helpful. | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
- | It has an advantage for programmers over the C preprocessor **#define** command in that it is understood & used by the compiler itself, not just substituted into the program text by the preprocessor before reaching the main compiler, so error messages are much more helpful. | + | ===== Using ' |
- | It also works with pointers but one has to be careful where **const** is put as that determines whether the pointer or what it points to is constant. | + | **Const** |
<code cpp> | <code cpp> | ||
Line 57: | Line 60: | ||
<WRAP info> | <WRAP info> | ||
**NOTE: | **NOTE: | ||
+ | |||
+ | * The constant pointer to a variable is useful for storage that can be changed in value but not moved in memory. | ||
</ | </ | ||
---- | ---- | ||
- | ===== Use of ‘const’ in Functions | + | ===== Use of ‘const’ in Function |
- | Of the possible combinations | + | A pointer (constant or otherwise) to a ‘const’ value can be very useful. |
- | + | ||
- | Even more useful is a pointer (constant or otherwise) to a ‘const’ value. | + | |
* This is useful for returning constant strings and arrays from functions which, because they are implemented as pointers, the program could otherwise try to alter and crash. | * This is useful for returning constant strings and arrays from functions which, because they are implemented as pointers, the program could otherwise try to alter and crash. | ||
* Instead of a difficult to track down crash, the attempt to alter unalterable values will be detected during compilation. | * Instead of a difficult to track down crash, the attempt to alter unalterable values will be detected during compilation. | ||
- | For example, if a function which returns a fixed ‘Some text’ | + | If a function which returns a fixed string is written like: |
<code cpp> | <code cpp> | ||
char *Function1() | char *Function1() | ||
{ | { | ||
- | return | + | return |
} | } | ||
</ | </ | ||
- | then the program could crash if it accidentally tried to alter the value doing: | + | ...then the program could crash if it accidentally tried to alter the value doing: |
<code cpp> | <code cpp> | ||
Line 85: | Line 88: | ||
</ | </ | ||
- | whereas the compiler would have spotted the error if the original function had been written: | + | **__The solution:__** |
<code cpp> | <code cpp> | ||
Line 94: | Line 97: | ||
</ | </ | ||
- | because the compiler would then know that the value was unalterable. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 100: | Line 105: | ||
===== Where it Gets Messy - in Parameter Passing ===== | ===== Where it Gets Messy - in Parameter Passing ===== | ||
- | When a subroutine or function is called with parameters, variables passed as the parameters might be read from in order to transfer data into the subroutine/ | + | ==== Passing |
- | + | ||
- | Some languages enable one to specify this directly, such as having ‘in:’, ‘out:’ & ‘inout: | + | |
- | + | ||
- | For example, a subroutine like: | + | |
<code cpp> | <code cpp> | ||
Line 113: | Line 114: | ||
</ | </ | ||
- | accepts the parameter | + | <WRAP info> |
+ | **NOTE: | ||
- | Therefore the subroutine can read the value of the variable passed to it but not alter it because any alterations it makes are only made to the copy and are lost when the subroutine ends. E.g. | + | * The subroutine can read the value of the variable passed to it but not alter it because any alterations it makes are only made to the copy and are lost when the subroutine ends. |
+ | * Here, the variable **original_value** would remain unchanged with a value of 5, and not be set to 96 after Subroutine2 is done. | ||
<code cpp> | <code cpp> | ||
+ | void Subroutine1() | ||
+ | { | ||
+ | original_value = 5; | ||
+ | Subroutine2(original_value); | ||
+ | } | ||
+ | |||
void Subroutine2(int Parameter1) | void Subroutine2(int Parameter1) | ||
{ | { | ||
Line 124: | Line 133: | ||
</ | </ | ||
- | would leave the variable it was called with unchanged not set to 96. | + | </ |
- | The addition of an **&** to the parameter name in C++ (which was a very confusing choice of symbol because an ‘&’ in front of variables elsewhere in C generates pointers!) causes the actual variable itself, rather than a copy, to be used as the parameter in the subroutine and therefore can be written to thereby passing data back out the subroutine. | + | |
+ | ---- | ||
+ | |||
+ | ==== Passing a parameter to a function by reference ==== | ||
+ | |||
+ | The addition of an **&** to the parameter name in C++ (which was a very confusing choice of symbol because an ‘&’ in front of variables elsewhere in C generates pointers!) causes the actual variable itself, rather than a copy, to be used as the parameter in the subroutine and therefore can be written to thereby passing data back out the subroutine. | ||
<code cpp> | <code cpp> | ||
Line 135: | Line 149: | ||
</ | </ | ||
- | would set the variable it was called with to 96. | + | <WRAP info> |
- | + | **NOTE: | |
- | **NOTE: | + | |
- | + | ||
- | That way of passing variables was a C++ addition to C. | + | |
- | + | ||
- | To pass an alterable variable in original C, a rather involved method was used. | + | |
- | + | ||
- | This involved using a pointer to the variable | + | |
+ | * To pass an alterable variable in original C, a rather involved method was used. | ||
+ | * This involved using a pointer to the variable as the parameter then altering what it pointed to was used. | ||
+ | * This works but requires the every use of the variable in the called routine altered like that and the calling routine also altered to pass a pointer to the variable. | ||
+ | * It is rather cumbersome. | ||
<code cpp> | <code cpp> | ||
void Subroutine4(int *Parameter1) | void Subroutine4(int *Parameter1) | ||
Line 152: | Line 163: | ||
</ | </ | ||
- | works but requires the every use of the variable in the called routine altered like that and the calling routine also altered to pass a pointer to the variable. It is rather cumbersome. | + | </ |
---- | ---- | ||
Line 158: | Line 169: | ||
==== But where does ‘const’ come into this? ==== | ==== But where does ‘const’ come into this? ==== | ||
- | Well, there is a second common use for passing data by reference or pointer instead of as a copy. | + | There is a second common use for passing data by reference or pointer instead of as a copy. |
- | That is when copying the variable would waste too much memory or take too long. | + | * That is when copying the variable would waste too much memory or take too long. |
+ | * This is particularly likely with large & compound user-defined variable types (‘structures’ in C & ‘classes’ in C++). | ||
- | This is particularly likely with large & compound user-defined variable types (‘structures’ in C & ‘classes’ in C++). So a subroutine declared: | + | So a subroutine declared |
<code cpp> | <code cpp> | ||
Line 168: | Line 180: | ||
</ | </ | ||
- | might be using **&** because it is going to alter the variable passed to it or it might just be to save copying time and there is no way to tell which it is if the function is compiled in someone else’s library. | + | might be using **&** because it is going to alter the variable passed to it; or it might just be to save copying time. |
- | This could be a risk if one needs to trust the subroutine not to alter the variable. | + | * There is no way to tell which it is if the function provided by an unknown library. |
+ | * This could be a risk if one needs to trust the subroutine not to alter the variable. | ||
To solve this, ‘const’ can be used in the parameter list. E.g. | To solve this, ‘const’ can be used in the parameter list. E.g. | ||
Line 181: | Line 194: | ||
* This is messy because it is essentially making an in-only variable passing method from a both-ways variable passing method which was itself made from an in-only variable passing method just to trick the compiler into doing some optimization. | * This is messy because it is essentially making an in-only variable passing method from a both-ways variable passing method which was itself made from an in-only variable passing method just to trick the compiler into doing some optimization. | ||
- | + | | |
- | | + | |
---- | ---- | ||
Line 190: | Line 202: | ||
In Object Oriented Programming, | In Object Oriented Programming, | ||
- | As well as the variables in the parameter list, the method has access to the member variables of the object itself which are always passed directly not as copies. | + | * As well as the variables in the parameter list, the method has access to the member variables of the object itself which are always passed directly not as copies. |
For example a trivial class, ‘Class1’, | For example a trivial class, ‘Class1’, | ||
Line 221: | Line 233: | ||
</ | </ | ||
- | which will ban Method1 in Class2 from being anything which can attempt to alter any member variables in the object. | + | * which will ban Method1 in Class2 from doing anything which can attempt to alter any member variables in the object. |
Of course one sometimes needs to combine some of these different uses of ‘const’ which can get confusing as in: | Of course one sometimes needs to combine some of these different uses of ‘const’ which can get confusing as in: | ||
Line 257: | Line 269: | ||
http:// | http:// | ||
+ | https:// |
c/c_const_declaration.1633376966.txt.gz · Last modified: 2021/10/04 19:49 by peter