Thread: concat overload +,-

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    concat overload +,-

    In main I want to concatenate two strings
    s1=s2+s3; // overloading +,=

    Code:
    String & String::operator=(const String & st)
    {
           if (this==&st)
              return *this;
           delete [] str;
           len=st.len;
           str new char[len+1];
           strcpy(str,st.str);
           return *this;
    }
    
    // assign a C string to a String
    String & String::operator=(const char * s)
    {
           delete [] str;
           len = strlen(s);
           str=new char[len+1];
           strcpy(str,s);
           return *this;
    }
    
    //concatinate two strings
    String operator+(const String &str1,const String &str2)
    {
           return strcat(str1.str,str2.str);
    }
    my questin concerns the data object that the pointer to char points to using the strcat function. Will this be destroyed when the overload function ends?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strcat() won't destroy either str1.str or str2.str, but you can destroy it/them in your function body if you so desire. str1 will change as result of call to strcat(), but str2 will not.



    based on code posted the str member of class String appears to be just large enough to hold str, without any extra space allocated for additional possible chars. Therefore, I think you need to create a new str1.str with enough space to hold both str1 and str2 when concatenated.


    obtain len of str1 and str2
    declare temp string of length equal to str1 and str2 combined + 1.
    copy str1.str to temp using strcpy.
    delete str1.str
    redeclare str1.str to be of same size as temp
    copy temp back to str1.str using strcpy()
    concatenate str2.str onto str1.str using strcat().

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    new concat

    Here is the new code based on your alg. Elad
    Thanks. Will you glance over it to see if I did it correctly

    I also removed the const in the definiton in order to chage str1
    and deleted the temp string.

    Code:
    String operator+(String &str1,const String &str2)
    {      
           temp=new char[str1.len+str2.len+1]
           strcpy(temp,str1.str);
           //resize str1.str to hold concatenate string
           delete [] str1.str;
           str1.str=new char[strlen(temp)];
           strcpy(str1.str,temp);
           delete [] temp;
           return strcat(str1.str,str2.str);
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think you need to declare a type for temp, probably char * like this:

    char * temp=new char[str1.len+str2.len+1];

    don't forget the semicolon at the end of the line. Reviewing it I guess temp only needs to be size str1.len + 1, since we aren't actually concatenating str2.str onto temp be copying temp back to str1.str. Oh well, giving temp the extra memory is of little consequence.

    Then you need to make room for the null char at the end of temp when declaring the new size for str1, as strlen() only indicates the length of non null char in the string passed to it.


    Given that you are sending two String objects to +, you probably should make the + operator a friend function rather than a class method in your class declaration.


    There's also a missing = sign in the = overloading function body, probably a typo.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    Here is the alternative you suggested
    Code:
    String operator+(String &str1,const String &str2)
    {      
           int size=str1.len+str2.len+1
           char * temp=new char[str1.len+1];
           strcpy(temp,str1.str);
           //resize str1.str to hold concatenate string
           delete [] str1.str;
           // had to create size before deletion of str1.str
           str1.str=new char[size];
           strcpy(str1.str,temp);
           delete [] temp;
           return strcat(str1.str,str2.str);// im worried about the return type
    }
    Here is my proto type
    friend String operator+(String & str1,const String &str2);

    finally String is a user defined class so returning a char * may not work or will the compiler type cast this to type String. Am i mistaken? Would the return type be String (user defined);determined by the first operand.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strcat(str1.str,str2.str);
    return str1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange concat bugg
    By Zarniwoop in forum C Programming
    Replies: 7
    Last Post: 05-01-2008, 09:43 PM
  2. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. Buffer Overload
    By xddxogm3 in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2003, 03:21 PM
  5. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 07:48 PM