reference...

S

Shraddha

As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....


#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}
 
R

Ron AF Greve

Hi,

It is not rereferenced. Instead the values of O2 are copied into O1. This is
done by the compiler supplied operator= (since you didn't specify your own
version of =).

So in the end you have two references pointing to two objects with the same
values (instead of two references pointing to the same object).


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
S

Szabolcs

Shraddha said:
As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....


#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}

Think about references as giving a new name to a variable. In your
example, the same variable can be referred to as r or o1. In fact you
are changing o1 to to value of o2.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

As references are behaving like a constant pointer...And we can not
change the value of reference once set...then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....

Not quite, or I'm misunderstanding you, you can not change what object
the reference refers to, but you can change the object (unless it's a
const reference).
#include<stdio.h>

You don't need this one
#include<iostream.h>

Use said:
#include<conio.h>

Non-standard header, please try not to post code that depends on non-
standard headers/libraries to this newsgroup.
class abc
{
public:
int x;
public:
abc()
{
}
abc(int y)
{
x=y;
}
/*void assign(int y)
{
x=y;
} */
};

int main()
{
abc o1(10),o2(20);
abc &r=o1;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
cout<<r.x;
getch();
return 0;
}


Try adding cout << o1.x before getch() and you'll see what happened.
Even though you commented out the assignment operator the compiler
generated one for you and that one is called when you do r = o2. So the
reference still refer to o1, but the value of o1.x has changed.
 
J

James Kanze

As references are behaving like a constant pointer...

The behave very, very much like a constant pointer that is
implicitly dereferenced with every use (at least in an
expression).
And we can not change the value of reference once set...

You cannot change what the reference refers to. Once
initialized, the reference IS what it refers to, the value of
the reference is the value of what it refers to, and you can
certainly change that.
then how is following program
works...This program gives the output as 10 and 20....Actually I was
expecting an error....

Let's rework it converting all of the uses of a reference into
the corresponding uses of a pointer:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

class abc
{
public:
int x;
public:
abc()
{}
abc(int y)
{
x=y;}
/*void assign(int y)
{
x=y;
} */
};
int main()
{
abc o1(10),o2(20);
abc &r=o1;

abc*const r = &o1 ;
clrscr();
cout<<r.x<<"\n\n";
r=o2;
*r = o2 ;
cout<<r.x;
getch();
return 0;
}

BTW: my systems don't have a header <conio.h>; to get clrscr()
and getch(), I have to include <termcap.h>, which is a
non-standard (but very wide spread) extention. When posting
code here, it's better to forget them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,377
Latest member
MableYocum

Latest Threads

Top