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.