Off-topic, put a white noise site Rainy Mood, when you can listen to the code, as if there is no
Anyway
In the post of C + + replication control, the function that mentions the operator is actually defined . This can actually be generalized to other languages, except that custom operators are forbidden in other languages.
First of all, a summary of the understanding: the operator is essentially a function , but different from the normal function, the standard alone specifies how to write how to call it.
Premise:
The underlying language implements operator definitions for built-in types, and does not allow the re-customization of operators of built-in types.
To ensure that operators of built-in types are not overloaded, the compiler restricts operator functions---this later.
Rule one:
The operator functions are operator plus operators as function names, such as operator+, operator=, and so on.
As long as the operator function is correctly defined (overloaded), the operator function is essentially called when the corresponding type goes to invoke the operator. -for example, copy control the assignment operator overloads mentioned in that article.
Such as:
#include <iostream>#include<string>#include<sstream>using namespacestd;classperson{Private: stringname; intAge ; Public: Person (string_name="Anonymous",int_age= -): Name (_name), age (_age) {}//constructors with default argumentsPersonoperator+(ConstPerson &Person_b) { person person;//defaultPerson.name = name + Person_b.name;//Person.age = age +Person_b.age; returnPerson ; } stringtoString () {Ostringstream out; out<<"name=["<<name<<"], age=["<<age<<"]"; return out. STR (); }};intMain () {person P1; person P2; Person P3,P4; P3=P1+P2;//operator +P4=p1.operator+ (p2);//cout<<"P3:"<<p3.tostring () <<Endl; cout<<"P4:"<<p4.tostring () <<Endl; return 0;}
Above,p1+p2 and p1.operator+ (p2) are exactly the same, that is, the compiler rules to encounter the + will go to call operator+ () . The other operators are the same.
------suddenly out of character, or continue to read the information it
C + + operator overloading