Operator overload

Source: Internet
Author: User

1. Differences between using a reload function as a member function and a friend function
A member function. One of the operands should be the class itself. During the call, the class object should be on the left, for example, c1 + 3. You can place the class in any position.
2. Overload ++, --; in the auto-increment (auto-subtraction) operator overload function, add an int-type parameter, that is, the Post-auto-increment (auto-increment) operator.
3. Reload stream insertion operator and stream extraction operator istream & operator> (istream &, custom class &) ostream & operator <(ostream &, custom class &) the overload of these two operators can only be used as friend functions or common functions, but cannot be defined as meta functions. Example: call operators when friend ostream & operator <(ostream &, Complex &); ostream & operator <(ostream & output, Complex & c) is overloaded <, when a member function is used for reload, the first parameter defaults to the object of this class, but for the io operator, the first parameter must be io, therefore, it can only be used as a binary friend function. This is because the compiler checks global functions and member functions during compilation. If there is an overload, it will connect to the overload operator, but the called object is still called, rather than the io is called, io is just a parameter.

4. Conversion constructor A: declare A class first. B: define a constructor with only one parameter in this class. The parameter type is the type to be converted, and the conversion method is formulated in the function body. C: In the scope of this class, you can use the following form for type conversion: class name (specified type of data)
5. Type conversion function: a type conversion function converts a class object to another type of data. Operator type name () {conversion statement}
<

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.