C + + overloaded operator operator

Source: Internet
Author: User

Operator is a C + + keyword that is used to extend C + +;

1. Operators that can be overloaded: new,new[],delete,delete[],+,-,*,/,%,^,&,|,~,!, =,<,>,+=,<<,>>,<<=,> >=,++,!=,<=,>=,&&,| |,++,--,->*,->, (), []

Operators that cannot be overloaded:. .* :: ?:

2. The base class pair assignment operator (=) overload cannot be inherited by a derived class.

"+" operator overloading: Typically, overloading a two-dollar operator, the operator overload function has only one parameter. Overloads a unary operator whose function does not require any arguments.

3. Overloading cannot change the precedence and syntax of an operator.

4, [],=, (),-> must be overloaded in the form of a class member function.

5. In addition to the memory management operator New,new[],delete,delete[], an operator that is overloaded in the form of a top-level function must contain an object of a class in his parameter table. When the operator is implemented with a top-level function, the reason for at least one class object parameter is to make the compiler-system-differentiated operator either built-in or user-definable.

Top-level functions: C + + contains a lot of header files, also contains many library functions, the top-level function is the object library function. When the top-level function overloads an operator, it is more than one parameter when the operator is overloaded with member functions. As long as you define a constructor that can be used for transformation, one advantage of overloading with the top-level function is that non-object operators can appear on the left side of the operator. When you use a class member function, the first operator must be the object of the class.

A friend function of a class can access the private members and protected members of the class. But the friend function is not a member function of the class. It can appear in any part of the class private, public, protected. (Use the Friend function only when overloading operators).

Overloaded operator++ operator

The C + + language has been extended to allow two forms of overloaded increment and decrement operators.
There is a syntactic problem, however, that the difference between overloaded functions depends on the difference in their parameter types, but there is only one argument for either increment or decrement prefixes or suffixes. To solve this language problem, C + + specifies that the suffix form has an int type parameter, and when the function is called, the compiler passes a value of 0 as the int parameter to the function:

classUpint {//"Unlimited precision int" Public: Upint&operator++();//+ + prefix   ConstUpintoperator++(int);//+ + suffixupint&operator--();//--Prefix   ConstUpintoperator--(int);//--Suffixupint&operator+=(int);//+ = operator, upints//and INTs phase operation...}; Upint i;++i;//call i.operator++ ();i++;//call i.operator++ (0);I.;//call i.operator--();i--;//call i.operator--(0);

There are some quirks in this specification, but you'll get used to it. In particular, it is important to note that these operator prefixes are different from the suffix form return value types. The prefix returns a reference with the suffix form returning a const type. Below we will discuss the prefix and suffix forms of the + + operator, which are also used with the--operator.

From the day you started working as a C programmer, you remember that the prefix form of increment is sometimes called "add and then retrieve", and the suffix form is called "Fetch and then add". These two sentences are very important, because they are the formal specification of increment prefixes and suffixes.

#include <iostream>using namespacestd;classa{ Public: A (): Value (0) {cout<<"I am Gouzao"<<Endl;} A (a&): Value (0) {cout<<"I am Gouzao"<<Endl;}; ~a () {cout<<"Xigou"<<Endl;} A&operator++() {cout<<"++a"<<Endl; ++value; return* This; }    ConstAoperator++(int) {cout<<"a++"<<Endl; A tmp=* This; Value++; returntmp; }    voidShow () {cout<<"Value:"<<value<<Endl; }Private:    intvalue;};intMain () {a A; A++;    A.show (); ++A;    A.show (); return 0;}

C + + overloaded operator operator

Related Article

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.