Overloading of increment operators

Source: Internet
Author: User

+ + increments each have pre-increment and post-increment differences, while overloaded operators, although the function names are the same, there should be a corresponding difference. The result of the increment operator before an integer variable is the same as the return value variable, and then the increment operator returns a temporary variable. It is also necessary to differentiate between the pre-increment and the post-increment when the increment operator overloads.

The pre-Increment action object is the same variable as the returned object, which requires that the argument be a reference to the object, and the return value is also a reference to the object when the first increment of the reaction object:

classname& operator++ ();//pre-increment operator set to class member function

Friend classname& operator++ (classname&a);//Pre-increment Fu set to class friend function.

The subsequent increment character also requires that the argument be a reference, because the arguments in the call are changed, but in order to differentiate from the previous increment, C + + provides a technical process:

ClassName operator++ (int);//post-volume operator set to class member function

Friend ClassName operator++ (className &a,int i); The friend function, after the call increment character violates the function parameter matches the general, but the compiler specially does the special processing.

For flow operators. Because the stream operator predecessor object parameter is Ostream&out, it is not an object of that class, so it cannot be set as a member function of a class, must be set to a friend function, and a friend function is not a member function of a class, but what is special about him is that he can access the private members of the class. So the operator overload of the stream must be declared as a friend function, such as friend ostream& operator (Classname&a);

Overloading of increment operators

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.