stl string problems with the + operator

This is a discussion on stl string problems with the + operator within the C++ Programming forums, part of the General Programming Boards category; I have created an IString class that wraps around stl string. When I test my classes + operator this is ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    stl string problems with the + operator

    I have created an IString class that wraps around stl string. When I test my classes + operator this is what I get:

    Code:
    IString sEdc= "Freddy Flinty";
    sEdc = "'" + sEdc + "'";
    cout<< "sEdc =" << sEdc<<endl;
    output: sEdc =Freddy Flinty''

    expected output: sEdc ='Freddy Flinty'

    Here is my operator definition

    Code:
    //buffer is the stl string private data member defined in the header file
    
    IString IString::operator + ( const IString &aString ) const
    {
    	IString result(buffer, aString);
    	return result;
    }
    
    IString IString::operator + ( const char *pString ) const
    {
    	IString result(buffer, pString);
    	return result;
    }
    
    IString operator + ( const char *pString, const IString &aString )
    {
      	IString result(aString.buffer, pString);
    	return result;
    }
    
    IString::IString(string str, const char* pString)
    {
    	buffer = str; //buffer is an stl string
    	buffer += pString;
    }
    Any ideas on what I am doing wrong?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    What happens when you step through it using the debugger?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    instead of

    sEdc = "'" + sEdc + "'";

    try this:

    sEdc = "foo" + sEdc + "bar";

    if what's happening is what I think is happening you should end up with

    Fred Flintyfoobar

    I think the left hand + sign calls this

    IString operator + ( const char *pString, const IString &aString )

    which then calls this:

    IString::IString(string str, const char* pString)


    The problem is that the rhs side of + becomes the first half of the new word no matter what and doesn't stay the second half like you expect it to. To correct this you could try overloading the constructor such that you can assign pString to buffer first if pString is lhs of + and add pString to buffer second if pString is rhs of +.

    IString::IString(const char* pString, string str)
    buffer = pString;
    buffer += str;

    IString::IString(string str, const char* pString)
    buffer = str;
    buffer += pString;

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    elad, thanks for the response. It works now! You were correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with string manipulation
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21