User Tools

Site Tools


c:c_const_declaration

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
c:c_const_declaration [2021/10/04 21:16] – [Passing a parameter to a function by value] peterc:c_const_declaration [2023/06/26 07:24] (current) – [Messier Still - in the Object Oriented Programming] peter
Line 140: Line 140:
 ==== Passing a parameter to a function by reference ==== ==== 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. 
-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. Therefore:+
  
 <code cpp> <code cpp>
Line 149: Line 148:
 } }
 </code> </code>
- 
-would set the variable it was called with to 96. 
  
 <WRAP info> <WRAP info>
-**NOTE:**  This method of passing a variable as itself rather than a copy is called a **reference** in C++. +**NOTE:**  This would set the variable it was called with to 96.
- +
-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 usedFor example+
  
 +  * 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.  For example:
 <code cpp> <code cpp>
 void Subroutine4(int *Parameter1) void Subroutine4(int *Parameter1)
Line 165: Line 162:
 } }
 </code> </code>
- 
-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. 
  
 </WRAP> </WRAP>
Line 199: 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, but C was designed for raw low-level programming on far less powerful computers than are standard these days so the programmer has to do it explicitly.+  * 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, but C was designed for raw low-level programming on far less powerful computers than are standard these days so the programmer has to do it explicitly.
  
 ---- ----
Line 238: Line 233:
 </code> </code>
  
-  * 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 274: Line 269:
 http://duramecho.com/ComputerInformation/WhyHowCppConst.html http://duramecho.com/ComputerInformation/WhyHowCppConst.html
  
 +https://www.cprogramming.com/tutorial/const_correctness.html
c/c_const_declaration.1633382203.txt.gz · Last modified: 2021/10/04 21:16 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki