C + + operator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.