0

I tried to overload the + operator on my own C styled version of string class.

Everything is fine except for the display() call for the second string s2 which displays garbage values.

class string_ {

   char *str;
   int len;

   public :
         ...
         void display()
         {
              cout << str;
         }
};

string_ :: string_()
{
   str = 0;
   len = 0;
}

string_ :: ~string_()
{
   delete []str;
   len = 0;
}


string_ :: string_(char *s,int l)
{
   len = l + 1; // +1 for \0
   str = new char[len];
   int i;
   for(i=0;i<len;i++)
          str[i] = s[i];
   str[i] = '\0';
}

string_ string_ :: operator +(string_ c)
{      
   int j = 0, i = 0;
   string_ s;
   s.len = len + c.len - 1;
   s.str = new char[s.len];
   while (str[i] != '\0')
   {s.str[i] = str[i]; i++;}

   while (c.str[j] != '\0')
   {s.str[i] = c.str[j]; i++; j++; }

   s.str[i] = '\0';
   //The below statements gives the desired output
   cout <<"\nIN operator +" << str;
   cout <<"\nIN operator +" << c.str;
   cout <<"\nIN operator +" << s.str;
   return s;
 }

int main()
{
   char *str = "Hello";
   char *str1 = " World";
   string_ s1(str,5);
   string_ s2(str1,6);
   string_ s3 = s1 + s2;
   cout << "\nstring s1 : ";
   s1.display();
   cout << "\nstring s2 : ";
   s2.display(); //PROBLEM
   cout << "\nConcatenated string : ";
   s3.display();
   return 0;
}
Zaid Khan
  • 786
  • 2
  • 11
  • 24
  • Anything wrong with std::string? (just curious) – Macmade Oct 02 '15 at 17:43
  • I know I should be using those but while teaching C styled strings to a cousin of mine it bugged me a lot as to why it produced garbage values. – Zaid Khan Oct 02 '15 at 17:45
  • It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Oct 02 '15 at 17:48
  • Please post output of your program – Aram Antonyan Oct 02 '15 at 17:49
  • 3
    See [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – R Sahu Oct 02 '15 at 17:54
  • @nathan I've been doing it for the last 15 days. The only warning I've received is of deprecation of c styled strings from g++. – Zaid Khan Oct 02 '15 at 17:56

1 Answers1

4

You are missing a proper copy-constructor where you clone the char-array from one string to another.

Also your operator+ should take a const-ref to your string class because a copy is not necessary here.

string_ string_ :: operator +(const string_& c)

The problem

Your 1st object has a pointer to a char-array, e.g. str=0x1. Because you have no copy-constructor the pointer-value is copied over automatically to the copy of your string. Two strings have now the same pointer. The first string deletes the array and the second will fail. Take a look at Rule of three which describes which functions should be implemented to avoid this problem. Edit: R Sahu was faster in the comments.

P.S. Since you use const-literals change char* to const char*.

P.S.2 Please post a MVCE next time.

Community
  • 1
  • 1
HelloWorld
  • 2,392
  • 3
  • 31
  • 68
  • To elaborate, your operator+ is taking it's argument by value which means it is copy constructing it. Since you did not implement a copy constructor, a default one is being generated. But the default one is wrong. – Anon Mail Oct 02 '15 at 17:53
  • Ok so 'string_ c' in 'operator+()' automatically gets deallocated when 'operator+()' – Zaid Khan Oct 04 '15 at 09:04