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 20:33] – peter | c:c_const_declaration [2023/06/26 07:24] (current) – [Messier Still - in the Object Oriented Programming] peter | ||
---|---|---|---|
Line 73: | Line 73: | ||
* 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 88: | Line 88: | ||
</ | </ | ||
- | whereas the compiler would have spotted the error if the original function had been written: | + | **__The solution:__** |
<code cpp> | <code cpp> | ||
Line 97: | Line 97: | ||
</ | </ | ||
- | because the compiler would then know that the value was unalterable. | + | <WRAP info> |
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 103: | 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 116: | 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 127: | Line 133: | ||
</ | </ | ||
- | would leave the variable it was called with unchanged not set to 96. | + | </ |
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== Passing a parameter | ||
- | 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. | + | 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 137: | Line 148: | ||
} | } | ||
</ | </ | ||
- | |||
- | would set the variable it was called with to 96. | ||
<WRAP info> | <WRAP info> | ||
- | **NOTE: | + | **NOTE: |
- | + | ||
- | 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 153: | Line 162: | ||
} | } | ||
</ | </ | ||
- | |||
- | 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 187: | 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. | ||
- | * Ideally, the programmer should not need control this detail of specifying exactly how variables are passed, just say which direction the information goes and leave the compiler to optimize it automatically, | + | * Ideally, the programmer should not need to control this detail of specifying exactly how variables are passed, just say which direction the information goes and leave the compiler to optimize it automatically, |
---- | ---- | ||
Line 226: | 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 262: | Line 269: | ||
http:// | http:// | ||
+ | https:// |
c/c_const_declaration.1633379618.txt.gz · Last modified: 2021/10/04 20:33 by peter