Thread: Why Does The destructor destroy the original object??

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok is this solution correct??

    Code:
    #include <string.h>
    #include <iostream.h>
    
    class str
    {
    	char *s;
    	public:
    		str(){}
    		~str()
    		{
    			delete []s;
    		}
    		str(str &p)
    		{
    			memcpy(this,&s,sizeof(s));
    			s=new char[strlen(p.s)+1];
    		}
    		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;
    }
    if yes! then now could you please explain me what am i doing when i call the xstrlen()??the control passes to copy constructor?but why??that i dont know?now please anybody explain me!what exactly is happening?
    Last edited by chottachatri; 03-12-2008 at 07:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM