1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Copy Constructor

Discussion in 'C++' started by royashish, Jul 30, 2007.

  1. royashish

    royashish New Member

    Joined:
    Jul 29, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    The purpose of this Code is to verify the need of copy constructor :
    Code:
    #include<iostream>
    using namespace std;
    
    class cpchk{
    private:
    	char* name;
    	int num;
    public:
    	// Methods for name
    	char* getname()const{
    		cout<<"Address:Name>>"<<&name<<endl;
    		return name;
    	}
    	void setname(const char* acname){
    		if(name==NULL)
    			name=(char*)malloc((strlen(acname)+1)*sizeof(char));
    		else{
    			free(name);
    			name=NULL;
    			name=(char*)malloc((strlen(acname)+1)*sizeof(char));
    		}
    		strcpy(name,acname);
    	}
    	
    	// Methods for num
    	int getnum()const{
    		cout<<"Address:Num>>"<<&num<<endl;
    		return num;
    	}
    	void setnum(const int inum){
    		num=inum;
    	}
    	
    	// Constructors
    	cpchk(){
    		cout<<"Constructor called"<<endl;
    		name=NULL;
    		num=0;
    	}
    	
    	cpchk(char* acname,int inum){
    		name=(char*)malloc((strlen(acname)+1)*sizeof(char));
    		strcpy(name,acname);
    	}
    	
    	// Destructor
    	~cpchk(){
    	cout<<"Address:Object>>"<<this<<endl;
    	cout<<"Destructor called"<<endl;
    	cout<<"Address:Name>>"<<&name<<endl;
    	cout<<"Address:Num>>"<<&num<<endl;
    	
    	if(name!=NULL)
    		free(name);
    		name=NULL;
    	}
    	
    	
    };
    
    int main(int argc,char** argv){
    	cpchk a,b;
    	a.setname("Hitesh");
    	a.setnum(10);
    
    	b.setname("Rajesh");
    	b.setnum(30);
    
    	cout<<endl<<"Before Assignent"<<endl;
    	cout<<"Object A"<<endl;
    	cout<<&a<<endl;
    	cout<<a.getname()<<endl;
    	cout<<a.getnum()<<endl;
    	
    	cout<<"Object B"<<endl;
    	cout<<&b<<endl;
    	cout<<b.getname()<<endl;
    	cout<<b.getnum()<<endl;
    
    	a=b;
    
    	cout<<endl<<"After Assignent"<<endl;
    	cout<<"Object A"<<endl;
    	cout<<&a<<endl;
    	cout<<a.getname()<<endl;
    	cout<<a.getnum()<<endl;
    	
    	cout<<"Object B"<<endl;
    	cout<<&b<<endl;
    	cout<<b.getname()<<endl;
    	cout<<b.getnum()<<endl;
    
    	return 0;
    }
    
    Op:

    Constructor called
    Constructor called

    Before Assignent
    Object A
    0012FF6C
    Address:Name>>0012FF6C
    Hitesh
    Address:Num>>0012FF70
    10
    Object B
    0012FF64
    Address:Name>>0012FF64
    Rajesh
    Address:Num>>0012FF68
    30

    After Assignent
    Object A
    0012FF6C
    Address:Name>>0012FF6C
    Rajesh
    Address:Num>>0012FF70
    30
    Object B
    0012FF64
    Address:Name>>0012FF64
    Rajesh
    Address:Num>>0012FF68
    30
    Address:Object>>0012FF64
    Destructor called
    Address:Name>>0012FF64
    Address:Num>>0012FF68
    Address:Object>>0012FF6C
    Destructor called
    Address:Name>>0012FF6C
    Address:Num>>0012FF70
    Press any key to continue


    I am getting an Assert Failed error , Along with the fact that the address for the string are different , which should not have been the case .

    Kindly comment .

    Regards ,

    Ashish
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    It's a very interesting problem and I will explain why you are getting an ASSERT failure.

    The steps you are doing as follows:
    1. Have 2 different objects
    2. Now you are assigning the object b to an object a
    3. Now in the destructor you are destroying the objects internal array name.

    Now what is happening is when you are assigning the address are getting copied and so when you are freeing for the second object its the address of the first object that is there in the name and so its already freed first time and so its giving an ASSERT failure.

    I hope I was clear a bit.
     

Share This Page