Thread: folllowing code should call the copy constructor and shud not give run time error!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    folllowing code should call the copy constructor and shud not give run time error!

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    class MyString
    {
    public: 
    	
    	MyString(const char *str="")
    	{
    		length = strlen(str);
    		name = new char[length + 1];
    		strcpy(name,str);
    	};
    	
    	//copy constructor
    	MyString(const MyString& o)
    	{
    		length=o.length;
    		name = new char[length+1];
    		strcpy(name,o.name);
    	};
    	
    	//desctructor
    	~MyString()
    	{
    		delete name;
    
    	};
    	void display()
    	{
    		cout<<"name: " << name << endl;
    	}
    private:
    	char *name;
    	int length;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	MyString* obj = new MyString("alex");
    	MyString* obj2=obj; 
    	
    	delete obj; // this line deleting the "name" in obj2. but it shud not.what is wrong       //in     my understanding.
    	obj2->display();
    
    	return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It shouldn't.

    You are not copying MyString instances, you are assigning pointers. Both pointers point to the same instance which you delete before accessing it through the other pointer.

    What you should be doing:

    Code:
    int main()
    {
    
    	MyString obj("alex");
    	MyString obj2(obj);
    
    	obj2.display();
    
    	return 0;
    }
    Note that you also need to overload the assignment operator to perform "deep copy-assignment".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    exactly....obj and obj2 are pointers not instances of MyString

    Thanks Anon,

    exactly....obj and obj2 are pointers not instances of MyString.
    how stupid sometime a person can get.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    MyString(const char *str="")
    {
    	length = strlen(str);
    	name = new char[length + 1];
    	strcpy(name,str);
    };
    	
    //copy constructor
    MyString(const MyString& o)
    {
    	length=o.length;
    	name = new char[length+1];
    	strcpy(name,o.name);
    };
    	
    //desctructor
    ~MyString()
    {
    	delete name;
    };
    You can get rid of those semicolons at the end of the functions... and your destructor needs to be
    Code:
    ~MyString()
    {
    	delete [] name;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Tags for this Thread