C + + operator overloading

Source: Internet
Author: User

Why overloading an operator:

The operands of operators in C + + predefined are limited to the basic built-in data types, but there is no way to manipulate our custom types (classes). But most of the time we need to perform similar operations on the types we define, and this time we need to redefine the operators and give them new capabilities to meet their needs.

1. General operator overloading

When you perform an operation between objects, the program calls the function that corresponds to the operator, so there are two ways of operator overloading: member functions and friend functions. The form of member functions is simple, which is to define an operator-related function within a class. Friend function because there is no this pointer, so the shape of the participation of one more.

1 classA2 {3     Public:4Aintd):d Ata (d) {}5A operator+ (a&);//member functions6A operator-(a&);7 8Friend A operator/(a&,a&);//friend function9Friend A operator% (a&,a&);Ten    Private: One      intdata; A }; - //form of the member function -A a::operator+ (A &a) the { -  returnA (data+a.data); - } -A a::operator-(A &a) + { -  returnA (data-a.data); + } A  at //form of a friend function -A operator/(a &a1,a &A2) - { -  returnA (a1.data/a2.data); - } -A operator% (a &a1,a &A2) in { -  returnA (a1.data%a2.data); to } + //then we can do a + 、-、 *,//For the object of the class.  - voidMainvoid) the { *A A1 (1), A2 (2), A3 (3); $a1=a2+A3;Panax Notoginseng  //or -a1=a2.operator+(A3); the}
2. Relational operator overloading

Relational operators have ==,!=,<,>,<=,>=.

BOOL operator = = (consta&! = (const a& < (  const a& <= ( const a&> ( const a&>= ( const a&);
3. Logical operator overloading

BOOL Operator | | (const a&&& (const a&!) ();
4. Single-Mesh operator overloading
a& operator + (); Here the +,-is positive or negative meaning, put in front of the object. A& Operator- (); A* operator & (); A& operator * ();
5. Increment or decrement operator overloading
a& operator + + (); // Front + + A operator + + (int); // Rear-facing + + a& operator--(); // Front-- A operator--(int); // Rear--
6. Bitwise operator Overloading
A operator | ( const a&& ( const a&^ (const a&<< (  int>> (int~ ();
7. Assignment operator overloading

a& operator + = (Consta& ); A& Operator-= (Consta& ); A& operator *= (Consta& ); A& operator/= (Consta& ); A& operator%= (Consta& ); A& operator &= (Consta& ); A& operator |= (Consta& ); A& operator ^= (Consta& ); A& operator <<= (inti); A& operator >>= (inti);
8. Memory operator Overloading
void New (size_t size); void New int i); void New [] (size_t size); void operator delete (void*p); void operator delete (voidintint  j); void operator delete [] (void* p);
9. Special operator overloading
a& operator = (Consta&);//The overloads of these operators can only be member functions. Charoperator [] (inti);//The return value cannot be a left valueConst Char*operator () (); T operator- ();//type conversion characteroperatorChar* ()Const; operatorint(); operatorConst Char()Const; operator Short int()Const; operatorLong Long()Const;//There's still a lot to be written.

And these can only be overloaded in the form of friend functions

Friend Inline ostream &operator << (ostream&, a&); // output stream friend inline IStream &operator >> (istream&, a&); // input Stream
10. Summary

Comparison of two overloaded methods:

    • In general, a single-mesh operator is best overloaded with a member function of a class, while a binocular operator is best overloaded with a friend function of a class.
    • Some of the following binocular operators cannot be overloaded with the friend function of a class: =, (), [], and.
    • A type conversion function can only be defined as a member function of a class and cannot be defined as a friend function of a class. C + + provides 4 types of conversion functions: reinterpret_cast (Implementing transformations during compilation), Const_cast (Implementing transformations during compilation), Stactic_cast (Implementing transformations during compilation), dynamic_cast (implementing conversions during run time) , and can return a flag that the conversion succeeds or not).
    • If the operation of an operator requires modifying the state of an object, it is preferable to select overloading as a member function.
    • If you want an implicit type conversion for the operand required by the operator (especially the first operand), you can only select the friend function.
    • When an operator function is a member function, the leftmost operand (or only the leftmost operand) must be a class object of the operator class (or a reference to the class object). If the operand on the left must be a non-homogeneous object, or an object of an intrinsic type, the operator function must be implemented as a friend function.
    • When an overloaded operator is required to be exchangeable, select overload as a friend function.

Precautions:

    1. All operators in C + + can be overloaded except for the generic relational operator ".", the member pointer operator ". *", The scope operator "::", the sizeof operator, and the three-mesh operator "?:".
    2. Overloaded operators restrict the allowed overloaded operators within the scope of an operator that is already in the C + + language, and cannot create new operators.
    3. Operator overloading is essentially a function overload, so the compiler's choice of operator overloading follows the selection principle of function overloading.
    4. An overloaded operator cannot change the precedence and binding of an operator, nor can it change the number of operator operands and the syntax structure.
    5. Operator overloading cannot change the meaning of the operator for an intrinsic type object. It can only be used with objects of user-defined types, or when mixed with objects of the user-defined type and internal types.
    6. Operator overloading is the proper modification of the original operator for the actual need for the new type of data, and the overloaded functionality should be similar to the original functionality, avoiding the use of overloaded operators without destination.

Resources:

Https://wuyuans.com/2012/09/cpp-operator-overload

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.