I don't know what's happening but added 3 constructors, destructors and also a move assignment operator and then created 3 objects with various tasks to test it out but just getting output as "parameterized constructor called" meaning only first object gets called. help!!I have added some code and modified several parts of it, but the post cannot be submitted. It is fine if this message appears when asking a new question, but it appears when I am trying to resubmit my edited post.
How can I solve this?
#include<iostream>
#include <cstring>
using namespace std;
class mystring{ //class definition
char *str;
public:
void display(){
cout<<str<<endl;
}
mystring():str(nullptr){ //Default constructor
cout<<"default constructor called"<<endl;
str=new char[1];
*str='\0';
}
mystring(const char *s):str(nullptr){ //Parameterized constructor
cout<<"parameterized constructor called"<<endl;
if(s==nullptr)
str=new char[1];
*str='\0';
str= new char[strlen(s) 1];
strcpy(str,s);
}
mystring(const mystring &rhs):str(nullptr){ //Copy constructor
cout<<"Copy constructor called"<<endl;
if(rhs.str==nullptr)
this->str=new char[1];
this->str[0]='\0';
str=new char[strlen(rhs.str) 1];
strcpy(str,rhs.str);
}
~mystring(){ //destructor
cout<<"Destructor called"<<endl;
delete[] str;
}
mystring &operator=(const mystring &rhs){ //Copy assignment
cout<<"Copy assignment"<<endl;
if(this==&rhs)
return *this;
delete[] str;
str=new char[strlen(rhs.str)];
strcpy(str,rhs.str);
return *this;
}
};
int main(){
mystring a("Damn");
mystring b=a;
a="hot damn";
mystring c(a);
a.display();
b.display();
c.display();
return 0;
}
CodePudding user response:
Why is my Move assignment operator not working?
There is no move assignment operator in the presented by you code. There is only the copy assignment operator that has a bug
mystring &operator=(const mystring &rhs){ //Copy assignment
cout<<"Copy assignment"<<endl;
if(this==&rhs)
return *this;
delete[] str;
str=new char[strlen(rhs.str)];
strcpy(str,rhs.str);
return *this;
}
You have to write
str=new char[strlen(rhs.str) 1];
instead of
str=new char[strlen(rhs.str)];
Also there are another bugs. For example in this constructor
mystring(const char *s):str(nullptr){ //Parameterized constructor
cout<<"parameterized constructor called"<<endl;
if(s==nullptr)
str=new char[1];
*str='\0';
str= new char[strlen(s) 1];
strcpy(str,s);
}
there is a memory leak. The memory allocated twice
str=new char[1];
str= new char[strlen(s) 1];
You should write
mystring(const char *s):str(nullptr){ //Parameterized constructor
cout<<"parameterized constructor called"<<endl;
if ( s == nullptr )
{
str=new char[1];
*str='\0';
}
else
{
str= new char[strlen(s) 1];
strcpy(str,s);
}
}
The same problem exists in the copy constructor. You have to write
mystring(const mystring &rhs):str(nullptr){ //Copy constructor
cout<<"Copy constructor called"<<endl;
str=new char[strlen(rhs.str) 1];
strcpy(str,rhs.str);
}
If you will make the changes then the program output will be
parameterized constructor called
Copy constructor called
parameterized constructor called
Copy assignment
Destructor called
Copy constructor called
hot damn
Damn
hot damn
Destructor called
Destructor called
Destructor called
CodePudding user response:
//Try to call mystring::mystring(const char*):
// pass a null pointer to the object data pointer
// V
mystring(const char *s):str(nullptr)
{
cout<<"parameterized constructor called"<<endl;
//if const char* == null,
//then allocate memory for object data
if(s==nullptr)
//This line will not be executed if you pass a string literal. Because a string literal does not point to null
str=new char[1];
//If the previous line failed, then you cannot access the data (points to null)
*str='\0';
str= new char[strlen(s) 1];
strcpy(str,s);
}
in main function:
//class Object Constructor paremeter: const char*
// V V V
mystring a("Damn");