diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/CPLUSPLUS/CONCEPT/reference_variable.html |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/CPLUSPLUS/CONCEPT/reference_variable.html')
-rw-r--r-- | reference/CPLUSPLUS/CONCEPT/reference_variable.html | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/reference/CPLUSPLUS/CONCEPT/reference_variable.html b/reference/CPLUSPLUS/CONCEPT/reference_variable.html new file mode 100644 index 0000000..f907323 --- /dev/null +++ b/reference/CPLUSPLUS/CONCEPT/reference_variable.html @@ -0,0 +1,167 @@ +<html> +<head> +<title>Reference variables</title> +</head> +<body bgcolor="#dddddd"> +<font color=brown> +<hr> +<center><h1>Reference Variables.</h1></center> +<hr> +<p> +Reference variables allow two variable names to address the +same memory location. The following example shows the +technique in its simplest form. + +<p> +<center> +<table border=1 width="80%" bgcolor="ivory"> +<tr> +<td> +<pre> + + #include <iostream.h> + + main() + { + int var1; + int &var2 = var1; // var2 is a reference variable. + + var1 = 10; + + cout << "var1 = " << var1 << endl; + cout << "var2 = " << var2 << endl; + } +</pre> +</td> +</tr> +</table> +</center> +<p> + + +Generally, you would not use a reference variable +in this way. Its more likely that they would be used +in function parameter lists to make the passing of +pointers more readable. +<p> +This gives C++ the ability to provide a +different approch to changing a variable from within a +function. Consider the two following programs. + +<p> +<center> +<table border=1 width="80%" bgcolor="ivory"> +<tr> + +<td> +<pre> + + #include <stdio.h> + + void Square(<b>int *pVal</b>); + + main() + { + int Number=10; + + printf("Number is %d\n", Number); + + Square(<b>&Number</b>); + + printf("Number is %d\n", Number); + } + + void Square(<b>int *pVal</b>) + { + <b>*pVal</b> *= <b>*pVal;</b> + + printf("Number is %d\n", <b>*pVal</b>); + } + +</pre> +</td> +<td> +<pre> + + #include <stdio.h> + + void Square(<b>int &Val</b>); + + main() + { + int Number=10; + + printf("Number is %d\n", Number); + + Square(<b>Number</b>); + + printf("Number is %d\n", Number); + } + + void Square(<b>int &Val</b>) + { + <b>Val</b> *= <b>Val</b>; + + printf("Number is %d\n", <b>Val</b>); + } + +</pre> +</td> +</tr> +</table> +</center> +<p> + +The program on the right should be clearer to read +because you do not need to worry about pointer +dereferencing. + +<hr> +<h2>Examples:</h2> +<img src="../../GRAPHICS/computer.gif" alt="o"> +<a href="..//EXAMPLES/refvar.cc">Example program.</a> +<p> + +<font color=brown> +<img src="../../GRAPHICS/computer.gif" alt="o"> +<a href="../../C/EXAMPLES/pointer_func.c">Example C program.</a> +</font> + +<hr> +<h2>See Also:</h2> + +</font> +<hr> +<font color=black> +<h2>C References</h2> +<p> +<img src="../../GRAPHICS/whiteball.gif" alt="o"> +<a href="../../C/CONCEPT/data_types.html">data types</a>. + +</font> + +<hr> +<p> +<center> +<table border=2 width="80%" bgcolor="ivory"> +<tr align=center> +<td width="25%"> +<a href="../cref.html">Top</a> +</td><td width="25%"> +<a href="../../C/master_index.html">Master Index</a> +</td><td width="25%"> +<a href="../SYNTAX/keywords.html">Keywords</a> +</td><td width="25%"> +<a href="../../C/FUNCTIONS/funcref.htm">Functions</a> +</td> +</tr> +</table> +</center> +<p> +<hr> +<font color=brown> +<address>Martin Leslie +29-Sep-96</address><p> +</font> +</body> +</html> |