0

here is mystr struct

#include <stdio.h>
#include <string.h>

struct mystr {
    char *str;

    mystr(const char *s) {
        str = new char[strlen(s) + 1];
        strcpy(str, s);
        printf("[1]%s\n", s);
    }

    mystr(const mystr &s) {
        str = new char[strlen(s.str) + 1];
        strcpy(str, s.str);
    }

    ~mystr() {
        delete[] str;
    }

    void printn() const {
        printf("%s\n", str);
    }

    //mystr& operator+=(const char *s) this line works!
    mystr operator+=(const char *s) {
        char *old = str;
        int len = strlen(str) + strlen(s);
        str = new char[len + 1];
        strcpy(str, old);
        strcat(str, s);
        printf("[2]%s,%s,%s\n", old, s, str);
        delete[] old;
        return *this;
    }
};

here is main code

int main() {
    mystr s = "abc";
    (s += "def") += "ghi";
    // only print  s = abcdef
    // when put & before operator+=, it prints s = abcdefghi
    printf("s = %s\n", s.str); 
}

Question

What is difference between return mystr & vs mystr? I see operator+= is called twice, but output is different. in c++ what behavior in return instance?

Community
  • 1
  • 1
kinjia
  • 41
  • 5

2 Answers2

2

The difference is if you return a copy or a reference.

With this:

mystr operator+=(const char*) {/* some code */ return *this;}

You return a copy of this object.

With this:

mystr& operator+=(const char*) {/* some code */ return *this;}

You return a reference to this object.

When you return a copy then this:

(s += "def") += "ghi";

Does not add "ghi" to s but to the copy of it, leaving s unchanged.

You can make your mystr(const mystr &s) copy constructor (and may be destructor) also to print out something to see the difference.

Öö Tiib
  • 10,809
  • 25
  • 44
  • I now get understanded copy, but before copy the original object's , the str has been add with 'ghi', ''' strcat(str, s); ''', before return *this; may be I misunderstand something. – kinjia Jan 07 '20 at 02:54
  • On case of lack of & original object is added "def" first in parentheses before copy is made then "ghi" is added to copy. – Öö Tiib Jan 07 '20 at 02:58
  • Why first += in parentheses can add def ,but the second += do not? What execute order of (s += "def") += "ghi"; – kinjia Jan 07 '20 at 03:02
  • Second += does add "ghi" not to s but to its copy that you return from first += – Öö Tiib Jan 07 '20 at 03:03
-1

Here difference you are asking about is due to return the temporary calculated value need to assign to left-hand variable. So, you have to continue the chain of assignments with return of reference (mystr&). If you return by value (mystr) then it breaks the chain of assignment.

  X& operator+=(const X& rhs) // compound assignment (does not need to be a member,
  {                           // but often is, to modify the private members)
    /* addition of rhs to *this takes place here */
    return *this; // return the result by reference
  }

https://en.cppreference.com/w/cpp/language/operator_assignment

Actually, whenever we overload the operator its developer responsibility to keep operator behavior the same as mentioned in language. As we develop the piece of code that can be used by thousands of users, each doesn't have time to check the implementation of our code.

The principle of least astonishment means that a component of a system should behave in a way that most users will expect it to behave; the behavior should not astonish or surprise users.

What are the basic rules and idioms for operator overloading?

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24