C + + operator overloading

Source: Internet
Author: User

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

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.