Overloading operator += => String and char

This is a discussion on Overloading operator += => String and char within the C++ Programming forums, part of the General Programming Boards category; Hi! I have another overloading operator problem. I want to use my String class with type char. Example: String temp ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Overloading operator += => String and char

    Hi!

    I have another overloading operator problem.

    I want to use my String class with type char.

    Example:
    String temp = "";
    char z = 'a';
    temp += z;

    .hpp file
    Code:
    String &operator+=(const char &character);
    .cpp file
    Code:
    String &String::operator+=(const char &character) {
    String newString = *this + character;
    setString(newString);
    return *this;
    }
    How can I make it work?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...and for some reason you deem this line:

    setString(newString);

    totally irrelevant?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You should implement + in terms of +=, not the other way round.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    How do I do that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Lets assume that your String class has the following data members:
    a : a pointer that to the memory used to store the data
    size : the amount of data being stored in a
    alloc : the amount of memory allocated to a
    Code:
    String &String::operator+=(const char &character) {
      // Note INDENTATION
      if ( size == alloc ) // Ok we need more memory
        getMoreMemory(); // Call to private member function that gets more memory for us
      a[size++] = character;
      return *this;
    }
    Then you simply have operator + like so
    Code:
    String operator+ (const String& s, const char &c) {
      return String(s) += c;
    }
    Now before you ask how to write getMoreMemory() I'd suggest you search the boards this time as I won't be so nice again.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Typo in the second code example, Thantos. Ought to be "operator +", not "operator +=".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    edit: Beaten to the typo..

    But note, that you should prefer to return a const object for operator + to prohibit behavior such as

    (String + String) = String

    So,
    Code:
    const String operator+(const String& s, const char& c)
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by CornedBee
    Typo in the second code example, Thantos. Ought to be "operator +", not "operator +=".
    Thanks, I just copied his header and forgot to remove the =

    Quote Originally Posted by MrWizard
    edit: Beaten to the typo..

    But note, that you should prefer to return a const object for operator + to prohibit behavior such as

    (String + String) = String

    So,

    Code:

    const String operator+(const String& s, const char& c)
    What? You example of
    Code:
    (String + String) = String
    Makes no sense since it wouldn't be legal.

    Thanks like saying:
    Code:
    int x = 5, y = 7, z = 10;
    x + y = z;
    Both would be illegal syntax. Adding the const on it doesn't change anything since its still by value.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Thantos
    Thanks, I just copied his header and forgot to remove the =


    What? You example of
    Code:
    (String + String) = String
    Makes no sense since it wouldn't be legal.

    Thanks like saying:
    Code:
    int x = 5, y = 7, z = 10;
    x + y = z;
    Both would be illegal syntax. Adding the const on it doesn't change anything since its still by value.
    Actually because in your example you don't return a const object you do let people write code like my example and it WILL compile. Of course it doesn't make sense , that's why you return a const object, to disallow that behavior! Here is a simple example if you still don't believe me:

    Code:
    struct MyInt
    {
      int m_val;
    
      MyInt(int val) : m_val(val) { }
      MyInt& operator+=(int val)
      {
    	m_val += val;
    	return *this;
      }
    };
    
    MyInt operator +(const MyInt lhs, int rhs)
    {
      return MyInt(lhs) += rhs;
    }
    
    int main(void)
    {
      MyInt v1(100), v2(200), v3(-1);
    
      (v1 + 10) = v3;
    }
    Play around with that and try switching to return a const object and note that it will fix the problem.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Thanks! It's working now.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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