This blog post is a long time ago, posted in my early a blog, Google today, found that there are many people reproduced, but it does not indicate the source. At that time think operator more fun. C++Sometimes it's a play-resistant thing. Operator it has two usages, one is operator overloading (operator overloading), and the other is operator casting (operation implicit conversion). 1.operatorOVERLOADINGC+ + can be overloaded with operator operators in the following format: type ToperatorOperator (), such as specific gravity load +, as follows [CPP] View plaincopytemplate<typename t>classA { Public: ConstToperator+ (Constt&RHS) { return This->m_ +RHS; } Private: T m_; }; Also such as the function object in STL, overloading (), this is C++The more recommended notation, the function is similar to the function pointer, as shown belowTemplate<typename t>structA {Toperator()(Constt& LHS,Constt& RHS) {returnlhs-RHS;} }; 2 operatorCASTINGC+ + can be implicitly converted via the operator overload format as follows:operatortype T (), as shown belowclassA { Public: operatorB* () {return This-B_;} operator ConstB* ()Const{return This-B_;} operatorb& () {return* This-B_;} operator Constb& ()Const{return* This-B_;} Private: B*b_; }; A; when if (a), compile, where it is converted to if (a.operatorB* ()) is, in fact, a judgmentif(a.b_)
C + + operator