Reference cannot be made to refer to a different variable

A

amit

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.
 
F

Fred Zwarts

amit said:
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.
 
A

amit

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...
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top