The unary address-of operator (&) takes the address of its operand. The address-of operator can be applied only to the following:
In the first two cases listed above, the result of the expression is a pointer type (an r-value) derived from the type of the operand. For example, if the operand is of type char, the result of the expression is of type pointer to char. The address-of operator, applied to const or volatile objects, evaluates to const type * or volatile type *, where type is the type of the original object.
The result produced by the third case, applying the address-of operator to a qualified-name, depends on whether the qualified-name specifies a static member. If so, the result is a pointer to the type specified in the declaration of the member. If the member is not static, the result is a pointer to the member name of the class indicated by qualified-class-name. (See Primary Expressions for more about qualified-class-name.) The following code fragment shows how the result differs, depending on whether the member is static:
class PTM
{
public:
int iValue;
static float fValue;
};
int PTM::*piValue = &PTM::iValue; // OK: non-static
float PTM::*pfValue = &PTM::fValue; // Error: static
float *spfValue = &PTM::fValue; // OK
In this example, the expression &PTM::fValue
yields type float *
instead of type float PTM::*
because fValue
is a static member.
The address of an overloaded function can be taken only when it is clear which version of the function is being referenced. See Address of Overloaded Functions for information about how to obtain the address of a particular overloaded function.
Applying the address-of operator to a reference type gives the same result as applying the operator to the object to which the reference is bound. The following program demonstrates this concept:
#include <iostream.h>
void main()
{
double d; // Define an object of type double.
double& rd = d; // Define a reference to the object.
// Compare the address of the object to the address
// of the reference to the object.
if( &d == &rd )
cout << "&d equals &rd" << "\n";
else
cout << "&d is not equal to &rd" << "\n";
}
The output from the program is always &d equals &rd
.