[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Reference cannot be made to refer to a different variable

amit

11/28/2008 7:02:00 AM

a trivial question. Just want your opinion if my understanding is
correct...

class A
{
int i;
public:
A(int temp):i(temp){ }
void show() { cout <<"value is : "<<i<<"\n";}
};
int main()
{
A a1(5);
A &objref = a1;

cout <<"Address before assignment.....\n";
objref.show();
cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<"\n";

A a2(12);
objref = a2; //planning/hoping/expecting to refer to different
object.

cout <<"Address AFTER assignment!!!\n";
objref.show();
cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<" ,&a2 :
"<<&a2 <<"\n";
return 0;
}


Output
===========
Address before assignment.....
value is : 5
&a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0

Address AFTER assignment!!!
value is : 12
&a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0 ,&a2 : 0xbfff9fdc



Observation
============
The default “operator =” function provided by the compiler is called
and (a1/objref) gets a copy of the values of a2.
objref still refers to the same object as a1.

Question
========
Am I right.
5 Answers

Paavo Helde

11/28/2008 8:06:00 AM

0

amit <amitkeerti@gmail.com> kirjutas:

> a trivial question. Just want your opinion if my understanding is
> correct...

Yes, references cannot be reseated, that's a good thing about them. Also,
they are not objects in C++ sense, meaning that you cannot legally obtain
the address or size of the reference itself.

If you want reseating you have to use pointers.

Paavo


Fred Zwarts

11/28/2008 8:07:00 AM

0

> "amit" <amitkeerti@gmail.com> wrote in message news:820f386f-26b6-4cee-846e-43304585a277@e1g2000pra.googlegroups.com...
> a trivial question. Just want your opinion if my understanding is
> correct...
>
> class A
> {
> int i;
> public:
> A(int temp):i(temp){ }
> void show() { cout <<"value is : "<<i<<"\n";}
> };
> int main()
> {
> A a1(5);
> A &objref = a1;
>
> cout <<"Address before assignment.....\n";
> objref.show();
> cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<"\n";
>
> A a2(12);
> objref = a2; //planning/hoping/expecting to refer to different
> object.
>
> cout <<"Address AFTER assignment!!!\n";
> objref.show();
> cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<" ,&a2 :
> "<<&a2 <<"\n";
> return 0;
> }
>
>
> Output
> ===========
> Address before assignment.....
> value is : 5
> &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0
>
> Address AFTER assignment!!!
> value is : 12
> &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0 ,&a2 : 0xbfff9fdc
>
>
>
> Observation
> ============
> The default “operator =” function provided by the compiler is called
> and (a1/objref) gets a copy of the values of a2.
> objref still refers to the same object as a1.
>
> Question
> ========
> Am I right.

Yes, correct. That is where references are for.
objref will always refer to the same object as a1, that is the definition of objref.
If you want to manipulate addresses, use pointers instead of references.


amit

11/28/2008 12:32:00 PM

0

On Nov 28, 1:05 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
> amit <amitkee...@gmail.com> kirjutas:
>
> > a trivial question. Just want your opinion if my understanding is
> > correct...
>
> Yes, references cannot be reseated, that's a good thing about them. Also,
> they are not objects in C++ sense, meaning that you cannot legally obtain
> the address or size of the reference itself.
>
> If you want reseating you have to use pointers.
>
> Paavo

paavo,
I am not sure if I agree. I got slightly confused.
A reference is like a second name of the same object.
when I do something like:
A &objref = a1,

it means the object has 2 names a1 and objref.
So i should be allowd to get the address of objref and I should also
be allowed to find the size and both should be same as a1.

Fo example: I have 2 names, one my official name in official records
and one my near and dear once call me. I will turn around if you call
me by either of the names...

Christian Hackl

11/28/2008 1:38:00 PM

0

amit ha scritto:

> On Nov 28, 1:05 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
>> amit <amitkee...@gmail.com> kirjutas:
>>
>> Yes, references cannot be reseated, that's a good thing about them. Also,
>> they are not objects in C++ sense, meaning that you cannot legally obtain
>> the address or size of the reference itself.
>>
> I am not sure if I agree. I got slightly confused.
> A reference is like a second name of the same object.
> when I do something like:
> A &objref = a1,
>
> it means the object has 2 names a1 and objref.
> So i should be allowd to get the address of objref and I should also
> be allowed to find the size and both should be same as a1.
>
> Fo example: I have 2 names, one my official name in official records
> and one my near and dear once call me. I will turn around if you call
> me by either of the names...

That's correct, but it does not contradict what Paavo said. "a1" and
"objref" are different names for the same object. Retrieving the address
or size of "objref" is the *same* as retrieving the address or size of "a1".

Consider it this way: the sheer fact that "objref" is a reference is
exposed by the language only at the place of its declaration. Once
you've established the reference, both names are completely equal in
their usage.

As for you real life example: I cannot make "amit" refer to a different
person once I've established that the name refers to you. Neither can I
do so with "a1" or "objref" in C++.


--
Christian Hackl

blargg.h4g

11/28/2008 3:39:00 PM

0

amit wrote:
> a trivial question. Just want your opinion if my understanding is
> correct...
[snip code trying to re-seat reference]

Trivial answer: http://www.parashift.com/c++-faq-lite/refer...