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