The inline facility is an addition to C++. C uses the preprocessor #define statement to provide
#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
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
int & rodents = rats;
But the & operator in the next statement
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