'Why do member functions that overload the + operator typically have 2 arguments when 1 produces the same result? [duplicate]
I've noticed that when people overload the + operator they often use two arguments when I've been able to simply pass in 1 and get the same result. What is the reasoning behind why the version with 2 arguments is preferred, when doing it the way with 1 seems to yield the same result?
Below are two segments from my code. (I am using the cctype library for the c string manipulation.)
One-argument version:
myString operator + (const myString& rhs){
int length = strlen(str) + strlen(rhs.str);
char* buff = new char[length +1];
strcpy(buff,str);
strcat(buff,rhs.str);
buff[length] = '\0';
myString temp(buff);
delete[]buff;
return temp;
}
Two-argument version:
friend myString operator + (const myString& lhs,const myString& rhs){
int length = strlen(lhs.str) + strlen(rhs.str);
char* buff = new char[length + 1];
strcpy(buff,lhs.str);
strcat(buff,rhs.str);
buff[length] = '\0';
myString temp(buff);
delete[] buff;
return temp;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
