Explain the function and usage of Address of operator in C++ programming

  • 2020-05-07 20:10:30
  • OfStack

grammar


& cast-expression

note
The 1-bit address-of operator (&) takes the address of its operand. The operand of the address-of operator can be either a function indicator or an lvalue specifying an object that is not a bit field and is not declared using the register store class specifier.
The address-of operator applies only to variables with a base, structure, class, or union type declared at the file scope level, or only to subscript array references. In these expressions, constant expressions that do not include the address-of operator can be added or extracted from the address-of expression.
When applied to a function or an lvalue, the result of this expression will be a pointer type derived from the operand type (rvalue). For example, if the operand is of type char, the result of the expression is a 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.
When the address-of operator is applied to a qualified name, the result depends on whether qualified-name specifies a static member. If so, the result is a pointer to the type specified in the member declaration. If the member is not static, the result is a pointer to name, a member of the class indicated by qualified-class-name.
The following code snippet illustrates the difference, depending on whether the member is static:


// expre_Address_Of_Operator.cpp
// C2440 expected
class PTM {
public:
      int  iValue;
  static float fValue;
};

int main() {
  int  PTM::*piValue = &PTM::iValue; // OK: non-static
  float PTM::*pfValue = &PTM::fValue; // C2440 error: static
  float *spfValue   = &PTM::fValue; // OK
}

In this example, since fValue is a static member, the expression &PTM::fValue produces type float * instead of type float PTM::*.
The address of the overloaded function can be taken only if the version of the function to be referenced is clear. For information on how to get the address of a particular overloaded function, see the address of an overloaded function.
By applying the address-of operator to the reference type, you get the same result as if the operator were applied to the object to which the reference is bound. Such as:


// expre_Address_Of_Operator2.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main() {
  double d;    // Define an object of type double.
  double& rd = d; // Define a reference to the object.

  // Obtain and compare their addresses
  if( &d == &rd )
   cout << "&d equals &rd" << endl;
}

Output


&d equals &rd

The following example USES the address-of operator to pass pointer arguments to functions:


// expre_Address_Of_Operator3.cpp
// compile with: /EHsc
// Demonstrate address-of operator &

#include <iostream>
using namespace std;

// Function argument is pointer to type int
int square( int *n ) {
  return (*n) * (*n);
}

int main() {
  int mynum = 5;
  cout << square( &mynum ) << endl;  // pass address of int
}

Output


25


Related articles: