Wednesday, June 24, 2009

It is possible to declare a REFERENCE to another variable



NOTE : -
THIS CONTENT IS NOT ORIGINAL

8. It is possible to declare a REFERENCE to another variable

It is possible to make one variable be another:


using namespace std;
#include

int main ()
{
double a = 3.1415927;

double &b = a; // b is a

b = 89;

cout << "a contains: " << a << endl; // Displays 89.

return 0;
}

Output
a contains: 89



(If you are used to pointers and absolutely want to know what happens, simply think double &b = a is translated to double *b = &a and all subsequent b are replaced by *b.)

The value of REFERENCE
b cannot be changed after its declaration. For example you cannot write, a few lines further, &b = c expecting that b is now c. It won't work. Everything is said on the declaration line of b. Reference b and variable a are married on that line and nothing will separate them.

References can be used to allow a function to modify a calling variable:


using namespace std;
#include

void change (double &r, double s)
{
r = 100;
s = 200;
}

int main ()
{
double k, m;

k = 3;
m = 4;

change (k, m);

cout << k << ", " << m << endl; // Displays 100, 4.

return 0;
}


Output
100, 4



If you are used to pointers in C and wonder how exactly the program above works, here is how the C++ compiler would translate it to C:


using namespace std;
#include

void change (double *r, double s)
{
*r = 100;
s = 200;
}

int main ()
{
double k, m;

k = 3;
m = 4;

change (&k, m);

cout << k << ", " << m << endl; // Displays 100, 4.

return 0;
}


Output
100, 4



A reference can be used to let a function return a variable:


using namespace std;
#include

double &biggest (double &r, double &s)
{
if (r > s) return r;
else return s;
}

int main ()
{
double k = 3;
double m = 7;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;

biggest (k, m) = 10;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10
cout << endl;

biggest (k, m) ++;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11
cout << endl;

return 0;
}


Output
k: 3
m: 7

k: 3
m: 10

k: 3
m: 11




Again, provided you're used to pointer arithmetic and if you wonder how the program above works, just imagine that the compiler translated it into the following standard C program:


using namespace std;
#include

double *biggest (double *r, double *s)
{
if (*r > *s) return r;
else return s;
}

int main ()
{
double k = 3;
double m = 7;

cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;

(*(biggest (&k, &m))) = 10;

cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;

(*(biggest (&k, &m))) ++;

cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;

return 0;
}


Output
k: 3
m: 7

k: 3
m: 10

k: 3
m: 11




To end with, for people who have to deal with pointers yet do not like it, references are useful to effectively un-pointer variables. Beware this is considered bad practice. You can get into trouble. See for example http://www.embedded.com/story/OEG20010311S0024.


using namespace std;
#include

double *silly_function () // This function returns a pointer to a double
{
static double r = 342;
return &r;
}

int main ()
{
double *a;

a = silly_function();

double &b = *a; // Now b is the double towards which a points!

b += 1; // Great!
b = b * b; // No need to write *a everywhere!
b += 4;

cout << "Content of *a, b and r: " << b << endl;

return 0;
}


Output
Content of *a, b and r: 117653




BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:

3G S

JAIL BREAK

iPHONE

BILL GATES

StEvE JoBs

steve jobs

STEVE JOBS

Steve Jobs

Love Country

SD

Burroughs

Lift

Joseph Johnson

Apple Rainbow

Apple Blue

Apple Laser Printer