Hello,
this is also one of the weirdest problem that i came across and even you would agree to it..the code is
Ok the 2nd A.putdata() prints garbage because when we come out of xstrlen the destructor gets called and destroys the object. That's fine but why does it destroy the original A object even though i have used call by value?? can anybody answer this to me??Code:#include <string.h> #include <iostream.h> class str { char *s; public: ~str() { delete s; } void getdata(char*); void putdata(); friend int xstrlen(str); }; int xstrlen(str A) { int l=0; while(*A.s) { l++; A.s++; } return l; } void str::getdata(char* p) { s=new char[strlen(p)+1]; strcpy(s,p); } void str::putdata() { cout<<s<<endl; } int main(void) { str A; A.getdata("string"); A.putdata(); xstrlen(A); A.putdata(); return 0; }