visual c++ - Operator= overloading in C++ -


The C ++ primer book has a code for the C-style character array, and shows how
= article 15.3 operator = in the operator.
  string & amp; String :: operator = (Const four * sobj) {// sobj is an empty pointer, if (! Sobj) {_size = 0; Remove [] _string; _string = 0; } And {_size = strlen (sobj); Remove [] _string; _string = new four [_size + 1]; Streakpie (_string, SOBJ); } Return * This; }   

Now I would like to know that any reference string & amp; Why is it necessary to return,

  zero string :: operator = (const char * sobj) {// sobj is an empty indicator, if (! Sobj) {_size = 0 ; Remove [] _string; _string = 0; } And {_size = strlen (sobj); Remove [] _string; _string = new four [_size + 1]; Streakpie (_string, SOBJ); }}   
  • Please help.

    It supports the following idioms:

      String A, B; Const char * c; // Set some interesting a = b = c;   

    To work for this, a proper object or context must be returned to assign b = c to a ; It is actually a = (b = c) according to the C ++ operator priority rules.

    If you take back the pointer this , then you have to write a = * (b = c) , which does not mean the desired meaning.

Comments