Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, February 23, 2011

C++ Data Types - Notes


8.1.1 Data Types <http://www.desy.de/gna/html/cc/Tutorial/node9.htm>

C++ introduces a new data type called reference. You can think of them as if they were ``aliases'' to ``real'' variables or objects. As an alias cannot exist without its corresponding real part, you cannot define single references. The ampersand (&) is used to define a reference. For example:
int ix;         /* ix is "real" variable */  
int &rx = ix;   /* rx is "alias" for ix */   
ix = 1;         /* also rx == 1 */   
rx = 2;         /* also ix == 2 */ 

References can be used as function arguments and return values. This allows to pass parameters as reference or to return a ``handle'' to a calculated variable or object.
The table 8.1 is adopted from [1] and provides you with an overview of possible declarations. It is not complete in that it shows not every possible combination and some of them have not been introduced here, because we are not going to use them. However, these are the ones which you will probably use very often.
Table 8.1:  Declaration expressions.

\begin{tabular} {\vert l\vert p{0.44\textwidth}\vert l\vert} \hline {\bf Declara...  ...returning reference to {\em type} & {\tt int \&count();} \\  \hline\end{tabular}

In C and C++ you can use the modifier const to declare particular aspects of a variable (or object) to be constant. The next table 8.2 lists possible combinations and describe their meaning. Subsequently, some examples are presented which demonstrate the use of const. 
Table 8.2:  Constant declaration expresssions.

\begin{tabular} {\vert l\vert p{0.4\textwidth}\vert} \hline {\bf Declaration} & ...  ...value}{\tt ;} &  constant pointer to constant {\em type} \\  \hline\end{tabular}

Now let's investigate some examples of contant variables and how to use them. Consider the following declarations (again from [1]):
int i;                        // just an ordinary integer   
int *ip;                      // uninitialized pointer to  integer   
int * const cp = &i;          // constant pointer to integer   
const int ci = 7;             // constant integer   
const int *cip;               // pointer to constant integer   
const int * const cicp = &ci; // constant pointer to constant integer 
The following assignments are valid:
i = ci;       // assign constant integer to integer   
*cp = ci;     // assign constant integer to variable                 
// which is referenced by constant pointer  
cip = &ci;    // change pointer to constant integer   
cip = cicp;   // set pointer to constant integer to                
// reference variable of constant pointer to                 
// constant integer 
The following assignments are invalid:
ci = 8;       // cannot change constant integer value   
*cip = 7;     // cannot change constant integer referenced                 
// by pointer   
cp = &ci;     // cannot change value of constant pointer   
ip = cip;     // this would allow to change value of constant integer 
*cip with *ip 
When used with references some peculiarities must be considered. See the following example program:

#include <stdio.h>    
int main() {     
const int ci = 1;     
const int &cr = ci;     
int &r = ci;    // create temporary integer for reference    
// cr = 7;      // cannot assign value to constant reference     
r = 3;          // change value of temporary integer     
print("ci == %d, r == %d\n", ci, r);     
return 0;  
} 
When compiled with GNU g++, the compiler issues the following warning:
conversion from `const int' to `int &' discards const
What actually happens is, that the compiler automatically creates a temporay integer variable with value of ci to which reference r is initialized. Consequently, when changing r the value of the temporary integer is changed. This temporary variable lives as long as reference r.
Reference cr is defined as read-only (constant reference). This disables its use on the left side of assignments. You may want to remove the comment in front of the particular line to check out the resulting error message of your compiler.

--


♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥

No comments:

Post a Comment