|
From: Peeyush B. <pee...@re...> - 2007-07-20 09:26:52
|
Hello All=0AI am facing one problem in code below. This code is for copy c=
onstructor.=0A=0AActaully when copy constructor gets called for get functio=
n with object b , the object value becomes 0. I think that that object b & =
object p are not pointing to same location as i explicitly declared the cop=
y constructor.=0A=0APlease explain why it is so... Why object b value becom=
e 0 . Also how to avoid this so that object value should print actual value=
.=0A=0A#include<iostream>=0Ausing namespace std;=0Aclass A{=0A stati=
c int count;=0A int *num;=0A public:=0A A():num(new in=
t(2)){=0A cout<<"Zero argument ctor called\n"<<endl;=0A =
count++;=0A }=0A=0A A(int i):num(new int(i)){=0A =
cout<<"One arument ctor called\n"<<endl;=0A co=
unt++;=0A }=0A=0A A(const A &i):num(i.num){=0A =
cout<<"Copy Ctor called\n"<<endl;=0A count++;=0A }=
=0A=0A void print(){=0A=0A cout<<"count"<<count<<"Val=
ue:"<<*num<<endl;=0A }=0A=0A ~A(){=0A cout<<"D=
tor called\t"<<"count"<<count--<<endl;=0A delete num;=0A =
}=0A};=0A=0Avoid get(A p){=0A cout<<"get function called=
\n"<<endl;=0A p.print();=0A }=0A=0A=0Aint A::count;=
=0A=0Aint main(){=0A=0AA a;=0Aa.print();=0AA b(3);=0Ab.print();=0Aget(b);=
=0Ab.print();=0AA c=3Da;=0Ac.print();=0Areturn 0;=0A}=0A=0A=0ARegards=0APee=
yush B.=0A=0A |