The inline facility is an addition to C++. C uses the preprocessor #define statement to provide macros, which are crude implementations of inline code. For example, here’s a macro for squaring a number:

#define SQUARE(X) X*X

This works not by passing arguments but through text substitution, with the X acting as a symbolic label for the “argument”:

a = SQUARE(5.0); is replaced by a = 5.0*5.0;

b = SQUARE(4.5 + 7.5); is replaced by b = 4.5 + 7.5 * 4.5 + 7.5;

d = SQUARE(c++); is replaced by d = c++*c++;

Only the first example here works properly. You can improve matters with a liberal application of parentheses:

#define SQUARE(X) ((X)*(X))

Still, the problem remains that macros don’t pass by value. Even with this new definition, SQUARE(c++) increments c twice, but the inline square() function in Listing 8.1 evaluates c, passes that value to be squared, and then increments c once.

The intent here is not to show you how to write C macros. Rather, it is to suggest that if you have been using C macros to perform function-like services, you should consider converting them to C++ inline functions.

Reference Variables

C++ adds a new compound type to the language—the reference variable. A reference is a name that acts as an alias, or an alternative name, for a previously defined variable. For example, if you make twain a reference to the clemens variable, you can use twain and clemens interchangeably to represent that variable. Of what use is such an alias? Is it to help people who are embarrassed by their choice of variable names? Maybe, but the main use for a reference variable is as a formal argument to a function. If you use a reference as an argument, the function works with the original data instead of with a copy. References provide a convenient alternative to pointers for processing large structures with a function, and they are essential for designing classes. Before you see how to use references with functions, however, let’s examine the basics of defining and using a reference. Keep in mind that the purpose of the following discussion is to illustrate how references work, not how they are typically used.

Creating a Reference Variable

You might recall that C and C++ use the & symbol to indicate the address of a variable. C++ assigns an additional meaning to the & symbol and presses it into service for declaring references. For example, to make rodents an alternative name for the variable rats, you could do the following:

int rats;

int & rodents = rats;    // makes rodents an alias for rats

In this context, & is not the address operator. Instead, it serves as part of the type identifier. Just as char * in a declaration means pointer-to-char, int & means reference-to-int. The reference declaration allows you to use rats and rodents interchangeably; both refer to the same value and the same memory location. Listing 8.2 illustrates the truth of this claim.

Listing 8.2. firstref.cpp

// firstref.cpp -- defining and using a reference

#include

int main()

{

    using namespace std;

    int rats = 101;

    int & rodents = rats;   // rodents is a reference

    cout << "rats = " << rats;

    cout << ", rodents = " << rodents << endl;

    rodents++;

    cout << "rats = " << rats;

    cout << ", rodents = " << rodents << endl;

// some implementations require type casting the following

// addresses to type unsigned

    cout << "rats address = " << &rats

    cout << ", rodents address = " << &rodents << endl;

    return 0;

}

Note that the & operator in the following statement is not the address operator but declares that rodents is of type int & (that is, it is a reference to an int variable):

int & rodents = rats;

But the & operator in the next statement is the address operator, with &rodents representing the address of the variable to which rodents refers:

cout <<", rodents address = " << &rodents << endl;

Here is the output of the program in Listing 8.2:

rats = 101, rodents = 101

rats = 102, rodents = 102

rats address = 0x0065fd48, rodents address = 0x0065fd48

As you can see, both rats and rodents have the same value and the same address. (The address values and display format vary from system to system.) Incrementing rodents by one affects both variables. More precisely, the rodents++ operation increments a single variable for which there are two names. (Again, keep in mind that although this example shows you how a reference works, it doesn’t represent the typical use for a reference, which is as a function parameter, particularly for structure and object arguments. We’ll look into these uses pretty soon.)

References tend to be a bit confusing at first to C veterans coming to C++ because they are tantalizingly reminiscent of pointers, yet somehow different. For example, you can create both a reference and a pointer to refer to rats:

int rats = 101;

int & rodents = rats;   // rodents a reference

int * prats = &rats    // prats a pointer

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги