c++ - crash happens while re-initializing a string object -


request have deep @ string class program present below

class string{ private: int len;          int size;          char *p; public:        int getlen()      {          return len;      }      int getsize()      {          return size;      }      char* getp()      {          return p;      }      string(char *p1=0)      {          cout<<"constructor called"<<endl;          if(p1!=0)          {             len=strlen(p1);             size=len+1;             p=new char(sizeof(char)*(size));             strncpy(p,p1,size);          }          else          {              len=size=0;              p="";          }      }      string(string &s1)      {          cout<<"copy constructor called"<<endl;          if(&s1!=this)          {              cout<<"copy constructor called"<<endl;              len=s1.getlen();              size=len+1;              p=new char(sizeof(char)*(size));              //char *d=s1.getp();              strncpy(p,s1.getp(),size);          }      }      void display()      {          cout<<"string len="<<len<<endl;          cout<<"string size="<<size<<endl;          cout<<"string name="<<p<<endl;      }      ~string()      {          cout<<"destructor called"<<endl;          if(strlen(p) >= 1)          {             cout<<"111"<<endl;             delete []p;          }          //cout<<"destructor finished"<<endl;      } }; int main(int argc, char *argv[]) {   string s1;   string s2("hello");   s1.display();   s2.display();   s2=string("hello world");   s2.display();   return 0; } 

please @ re-initialization line

     s2=string("hello world"); 

string("hello world") create temporary string object , should call assignment operator. since default assignment operator compiler shallow copy, after temporary string object goes out of scope char pointer present in string class become null.

i tried overload assignment operator below

 string& operator =(string &s1)  {      cout<<"operator = called"<<endl;      if(&s1!=this)      {          len=s1.len;          size=s1.size;          if(strlen(p)>=1)               delete p;          p=new char(sizeof(char)*size);          strncpy(p,s1.getp(),size);      }      return *this;   } 

but giving error no match ‘operator=’ (operand types ‘string’ , ‘string’) s2=string("world");

request compile program after correcting 'operator=', because doubt copy constructor might give error..

thanks in advance..

one problems line:

p=new char(sizeof(char)*(size)); 

you need use square brackets instead:

p=new char[sizeof(char)*(size)];           ^                   ^ 

second, copy constructor , assignment operator, argument type should const-qualified references, aka const string&, not string&. also, practice, methods don't modify member variables (such display or getp) should have const @ end of function declaration (i.e. before {).


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -