22. The Microsoft Foundation Class Library: Fundamentals

Chapter 18 - Complete I/O in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Reference Variables
The reference variable is a C++ feature that can really be appreciated because it simplifies the syntax and readability of the more confusing pointer notation. Remember that by using pointer parameters, a program could pass something to a function either call-by-reference or call-by-variable, which enabled the function to change the item passed. In contrast, call-by-value sends a copy of the variable’s contents to the function. Any change to the variable in this case is a local change not reflected in the calling routine.
In the next example, the program passes an stStudent structure to a function using the three possible calling methods: call-by-value, call-by-reference with pointer notation, and call-by-reference using the simpler C++ reference type. If the program were sending the entire array to the subroutine, by default the array parameter would be passed call-by-reference. However, single structures within the array, by default are passed call-by-value.
//
//  refvar.cpp
//  C++ program demonstrating how the C++ reference type
//  eliminates the more confusing pointer notation.
//  The program also demonstrates how to pass a single
//  array element, call by value, variable, and reference.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
struct stStudent {
 char   pszName[66],
        pszAddress[66],
        pszCity[26],
        pszState[3],
        pszPhone[13];
 int    icourses;
 float  GPA;
};

void vByValueCall     (stStudent   stAStudent);
void vByVariableCall  (stStudent *pstAStudent);
void vByReferenceCall (stStudent &rstAStudent);

void main(void)
{
 stStudent astLargeClass[100];
 astLargeClass[0].icourses = 10;

 vByValueCall     ( astLargeClass[0]);
 cout << astLargeClass[0].icourses << “\n”; // icourses still 10

 vByVariableCall  (&astLargeClass[0]);
 cout << astLargeClass[0].icourses << “\n”; // icourses = 20

 vByReferenceCall ( astLargeClass[0]);
 cout << astLargeClass[0].icourses << “\n”; // icourses = 30
}
void vByValueCall(stStudent  stAStudent)
{
 stAStudent.icourses += 10;    // normal structure syntax
}

void vByVariableCall(stStudent *pstAStudent)
{
 pstAStudent->icourses += 10;  // pointer syntax
}

void vByReferenceCall(stStudent &rstAStudent)
{
 rstAStudent.icourses += 10;   // simplified reference syntax
}
Notice that the following portion of code has spliced together each function’s prototype, along with its matching invoking statement:
void vByValueCall     (stStudent  stAStudent);
    vByValueCall     ( astLargeClass[0]    );

void vByVariableCall  (stStudent *pstAStudent);
    vByVariableCall  (&astLargeClass[0]     );

void vByReferenceCall (stStudent &rstAStudent);
    vByReferenceCall ( astLargeClass[0]     );
One immediate advantage of this style is the simpler syntax needed to send a reference variable, astLargeClass[0] (the last statement), over the equivalent pointer syntax, &astLargeClass[0]. At this point, the difference may appear small. However, as your algorithms become more complicated, this simpler syntax can avoid unnecessary precedence-level conflicts with other operators such as the pointer dereference operator (*) and the period member operator (.), which qualifies structure fields.
The next three statements were pulled out of the program’s respective functions to show the syntax for using the structure within each function:
stAStudent.icourses   += 10;  // normal structure syntax
pstAStudent->icourses += 10;  // pointer syntax
rstAStudent.icourses  += 10;  // simplified reference syntax
The last two statements make a permanent change to the passed stStudent structure because the structure was passed call-by-reference (variable). Notice that the last statement did not require the pointer operator.
The difference between the first and third statements is dramatic. Although they look identical, the first statement references only a copy of the stStudent structure. In this case, when stAstudent.icourses is incremented, it is done only to the function’s local copy. Exiting the function returns the structure to bit oblivion, along with the incremented value. This explains why the program outputs 10, 20, 30, instead of 20, 30, 40.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net