Home > Back-end >  String overload operation, when to return to the String when return String &?
String overload operation, when to return to the String when return String &?

Time:10-01

Below is the String class reloading, there is a question when return String when return String& ?


String operator + (const String & amp; STR) const;//overloaded +
String& Operator=(const String & amp; STR);//overloaded=
String& The operator +=(const String & amp; STR);//overloaded +=

CodePudding user response:

Return what see yourself to achieve what logic, but regular, common int, for example:
 
//add operation operator +
Int a=1, b=2, c;
C=a + b;//a as the caller of the=operator, b is const parameter must be constant, c is the return value, this sentence, you want a value to be changed? Returns a reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking, to introduce the concept of value about, you want a + b expression the return value is the left or the right value? You can try to take the address & amp; (a + b) can be compiled,

 
Int a;
A=1;//this sentence is also an assignment expression, it has a return value, the return value is the variable a itself, is an lvalue, can get the address, also can be assigned to other variables, it is
Int b=a=1;//equivalent to int b=(a=1), first to a value of 1, then a value is assigned to b, then a and b are 1.
(a=1)=2;//grammar can also be so, but it's also a wonderful work, put 1 is assigned to a, returns the variable a itself, then give a 2 assignment,

What return, general just to cater to the syntax of the normal thinking, you can have a wonderful work, responsibility,
More do not say, is to return to the reference or not reference, diy writing code to study more, you can put your ideas are made up with code, to verify the result of you, to know,

CodePudding user response:

Upstairs said is very good, why want to use & amp; , you must have used cin, use the cin continuous input a series of variable
For example,
Cin> A> b> c;
This line of code is the use & amp; Cin> A return to a reference of cin, and then continue to use cin> B

CodePudding user response:

reference 1st floor SDGHCHJ response:
return what see yourself to achieve what logic, but regular, common int, for example:
 
//add operation operator +
Int a=1, b=2, c;
C=a + b;//a as the caller of the=operator, b is const parameter must be constant, c is the return value, this sentence, you want a value to be changed? Returns a reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking, to introduce the concept of value about, you want a + b expression the return value is the left or the right value? You can try to take the address & amp; (a + b) can be compiled,

 
Int a;
A=1;//this sentence is also an assignment expression, it has a return value, the return value is the variable a itself, is an lvalue, can get the address, also can be assigned to other variables, it is
Int b=a=1;//equivalent to int b=(a=1), first to a value of 1, then a value is assigned to b, then a and b are 1.
(a=1)=2;//grammar can also be so, but it's also a wonderful work, put 1 is assigned to a, returns the variable a itself, then give a 2 assignment,

What return, general just to cater to the syntax of the normal thinking, you can have a wonderful work, responsibility,
More do not say, is to return to the reference or not reference, diy writing code to study more, you can put your ideas are made up with code, to verify the result of you, know,

Teacher speak of good, gains

CodePudding user response:

Mistakes

CodePudding user response:

With what they achieve what logical relationship is not big, but is the correctness and efficiency of the semantic relative

CodePudding user response:

If you do not include the field points to the memory of class, what exactly do you return a string or string& , there is little difference between

CodePudding user response:

refer to 6th floor truth is right or wrong response:
if you do not include the field points to the memory of class, what exactly do you return a string or string& , there is little difference between


Within the string is a function to the value of the copy again a out

String& Is the reference directly return, there is no copy

I understand, right

CodePudding user response:

refer to the second floor Italink response:
upstairs is very good, why want to use & amp; , you must have used cin, use the cin continuous input a series of variable
For example,
Cin> A> b> c;
This line of code is the use & amp; Cin> A return to a reference of cin, and then continue to use cin> B


"

//add operation operator +
Int a=1, b=2, c;
C=a + b;//a as the caller of the=operator, b is const parameter must be constant, c is the return value, this sentence, you want a value to be changed? Returns a reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking, to introduce the concept of value about, you want a + b expression the return value is the left or the right value? You can try to take the address & amp; (a + b) can be compiled,
"

His this period I don't understand, "if the return reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking," what is the cause of this sentence?

CodePudding user response:

reference 1st floor SDGHCHJ response:
return what see yourself to achieve what logic, but regular, common int, for example:
 
//add operation operator +
Int a=1, b=2, c;
C=a + b;//a as the caller of the=operator, b is const parameter must be constant, c is the return value, this sentence, you want a value to be changed? Returns a reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking, to introduce the concept of value about, you want a + b expression the return value is the left or the right value? You can try to take the address & amp; (a + b) can be compiled,


I'm not too understand this sentence: "if you go back reference, then you can have (a + b)=4 which do not conform to the conventional thinking of SAO," why return reference (a + b)=4?

CodePudding user response:

references 9 f bandaoyu response:
I'm not too understand this sentence: "if you go back reference, then you can have (a + b)=4 SAO operations that do not conform to the conventional thinking," why return reference (a + b)=4?


I said, is the semantic problems,
Operator + is to return a reference to the right value, semantic problems;
Operator=is to return to the reference, the efficiency problem, such as someone upstairs said,
 
String operator + (const String& S) {
Return * this;
}

String& Operator=(const String& S) {
Return * this;
}



No problem on grammar code, semantic chaos,
 
//returns a reference type, who returned to that of reference?? Can only be * this, then you can write d=(a + b)=c this code,
String& The operator + (const String& S) {
Return * this;
}

//return a reference, you will get more calls a copy tectonic String (const String& S), inefficient
String operator=(const String& S) {
Return * this;
}

//not return a value, it will not be able to write a=b=c, so that the code of the assignment like,
Void operator=(const String& S) {

}
  • Related