Differences Between Address and reference in C ++

Source: Internet
Author: User

See this example. Use vc6.0

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Void swap2 (Int & A, Int & B); // Declaration; reference
Void swap3 (int * a, int * B );

Void swap2 (Int & A, Int & B) // definition; reference
...{
Int temp =;
A = B;
B = temp;
};

Void swap3 (int * a, int * B)
...{
Int P = *;
* A = * B;
* B = P;
}

Int main (INT argc, char * argv [])
...{
/**///////////////////////////////////// ////////////
Int A = 2, B = 10;
Cout <A <"," <B <Endl;
// Swap2 (& A, & B); // No
Swap2 (a, B); // reference
// Swap2 (a + 1, B); // No
Cout <A <"," <B <Endl;

Int * pA, * pb;
Pa = & A; // address
PB = & B;
Cout <* Pa <"," <* pb <Endl;
// Swap2 (Pa, Pb); // No ()
Swap2 (* pA, * pb); // reference
Cout <* Pa <"," <* pb <Endl;
/**///////////////////////////////////// /////////////

Int c = 6, D = 3;
Cout <C <"," <D <Endl;
Swap3 (& C, & D); // address
Cout <C <"," <D <Endl;


Int * PC, * PD;
PC = & C; // address
Pd = & D;
Cout <* Pc <"," <* PD <Endl;
Swap3 (PC, PD );
// Swap3 (PC + 1, PD); // (B) It can be compiled, but it is obviously wrong and dangerous. It can be compared with ()
Cout <* Pc <"," <* PD <Endl;

/**///////////////////////////////////// //////////////////

C = 23; D = 67;
// Int & E, & F; // No. The referenced variable must be initialized.
Int & E = C, & F = D; // reference; it can be understood that E is the alias of C.

Cout <e <"," <F <Endl;
Swap2 (E, F );
Cout <e <"," <F <Endl;

Cout <e <"," <F <Endl;
Swap3 (& E, & F); // address
Cout <e <"," <F <Endl;

Return 0;
}

My personal experience:

// When "&" is used in the Declaration and definition, it is a reference; when "&" is used in the Execution Code, it is an address.
// However, for the following statements:
// Int * Pc = & C;
// Still the address. This initialization action also executes code.

// In fact, references, addresses, and pointers are closely related. Such as Int & A, const int * P, int * const P, const int * const P and
// Handle and so on can be understood as limiting pointer operations to avoid pointer drawbacks.
// It can be seen from (a) and (B) that the reference should be safer.

// Please correct.

Related Article

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.