Explain the function and usage of address-of operator & in C + + programming _c language

Source: Internet
Author: User
Tags volatile

Grammar

& Cast-expression

Note
The unary address-of operator (&) takes the address of its operand. The operands of the address-of operator can be either a function indicator or the left value of the object that specifies that it is not a bit field and is not declared using the Register storage class descriptor.
The address-of operator applies only to variables with a base, struct, class, or union type declared at the file-scope level, or only to subscript array references. In these expressions, you can add or extract constant expressions that do not include the address-of operator in an address-of expression.
When applied to a function or a left value, the result of the expression is the type of pointer derived from the operand type (the right value). 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 a const or volatile object) evaluates to either the const type * or the volatile type *, where type is the original object.
When you apply the address-of operator to a qualified name, the result depends on whether the 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 the member name of the class indicated by Qualified-class-name.
The following code snippet illustrates the difference between the results, 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, because Fvalue is a static member, the expression &ptm::fvalue produces the type float * instead of the type float PTM::*.
You can take the address of an overloaded function only when you explicitly specify the version of the function you want to reference. For information about how to obtain the address of a particular overloaded function, see the address of an overloaded function.
By applying the address-of operator to a reference type, you get the same result as applying the operator to the object to which the reference is bound. For example:

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" << end l;
}

Output

&d equals &rd

The following example uses the address-of operator to pass pointer arguments to a function:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.