[flydream@flydream ThinkingInC++]$ cat PassReference.cpp
#include <iostream>
using namespace std;
void passPointer(int *P)
{
cout << "P = " << P << endl;
cout << "*P = " << *P << endl;
*P = 100;
}
void passReference(int &R)
{
cout << "R = " << R << endl;
cout << "&R = " << &R << endl;
R = 200;
}
int main(int argc, char **argv)
{
int P = 10, R = 20;
cout <<"======================= PASS POINTER ===================" << endl;
cout << "P = " << P <<endl;
cout << "&P = " << &P <<endl;
passPointer(&P);
cout << "execute Function later : P = " << P << endl;
cout <<"======================= PASS REFERENCE ===================" << endl;
cout << "R = " << R <<endl;
cout << "&R = " << &R <<endl;
passReference(R);
cout << "execute Function later : R = " << R << endl;
return 0;
}
[flydream@flydream ThinkingInC++]$ g++ PassReference.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
======================= PASS POINTER ===================
P = 10
&P = 0xbfb2ed0c
P = 0xbfb2ed0c
*P = 10
execute Function later : P = 100
======================= PASS REFERENCE ===================
R = 20
&R = 0xbfb2ed08
R = 20
&R = 0xbfb2ed08
execute Function later : R = 200
[flydream@flydream ThinkingInC++]$
#include <iostream>
using namespace std;
void passPointer(int *P)
{
cout << "P = " << P << endl;
cout << "*P = " << *P << endl;
*P = 100;
}
void passReference(int &R)
{
cout << "R = " << R << endl;
cout << "&R = " << &R << endl;
R = 200;
}
int main(int argc, char **argv)
{
int P = 10, R = 20;
cout <<"======================= PASS POINTER ===================" << endl;
cout << "P = " << P <<endl;
cout << "&P = " << &P <<endl;
passPointer(&P);
cout << "execute Function later : P = " << P << endl;
cout <<"======================= PASS REFERENCE ===================" << endl;
cout << "R = " << R <<endl;
cout << "&R = " << &R <<endl;
passReference(R);
cout << "execute Function later : R = " << R << endl;
return 0;
}
[flydream@flydream ThinkingInC++]$ g++ PassReference.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
======================= PASS POINTER ===================
P = 10
&P = 0xbfb2ed0c
P = 0xbfb2ed0c
*P = 10
execute Function later : P = 100
======================= PASS REFERENCE ===================
R = 20
&R = 0xbfb2ed08
R = 20
&R = 0xbfb2ed08
execute Function later : R = 200
[flydream@flydream ThinkingInC++]$