Operator Overloading for C ++ notes

Source: Internet
Author: User
Tags bitwise operators

Pay attention to the heavy-duty operators

(1) The overload operator must have a class type operand. Built-in operators cannot be overloaded.


[Cpp]
Operator + (int, int); // This is incorrect and is of built-in type.
Operator + (int, classType); // This can be changed to ensure a custom type.

Operator + (int, int); // This is incorrect and is of built-in type.
Operator + (int, classType); // This can be changed to ensure a custom type.
(2) The overload operator can be defined as a common non-member function or a member function of a class. A member function defined as a class contains a this pointer by default as a parameter. When a non-member function is defined, it is usually necessary to set them as the friends of the operation class.

(3) There are two methods to call the overload OPERATOR: implicit call, showing the call.


[Cpp]
Cout <item1 + item2 <endl; // call the overload operator implicitly
Cout <operator + (item1, item2); // overload operator of non-member functions, showing calls
Cout <item1.operator + (item2); // overload operator of the member function, displaying the call

Cout <item1 + item2 <endl; // call the overload operator implicitly
Cout <operator + (item1, item2); // overload operator of non-member functions, showing calls
Cout <item1.operator + (item2); // overload operator of the member function, displaying the call


Several criteria for overloaded operators

(1) When defining heavy-duty operators, reload them according to the conventional habits. For example, "=" can be overloaded to test whether two objects are equal;

(2) If a class contains arithmetic operators or bitwise operators, it should provide the corresponding compound value assignment operator overload.

(3) the class that will be associated with the container key type will be used to define the "<" Operator for heavy loading. Because the associated container uses the <operator of the key type by default. Even if the type will only exist in the ordered container, classes should usually define equal (=) operators and smaller than (<) operators, on the grounds that many algorithms assume that these operators exist. For example, sort uses "<" and find uses "= ". If the class defines the "=" operator, it should also define "! = "Operator. The class defines "<" or ">,>=, <=, <".

(4) how to select a member function or a common function: assign value (=), subscript [], call (), member access arrow->, compound assignment, ++, --, and so on are defined as class members. symmetric operators and comparison operators are best defined as common functions (Class membership functions ).

 

Several common overload Methods

(1) input and output operators


[Cpp]
Friend ostream & operator <(ostream & out, const classType & obj );
Template <class T>
Friend ostream & operator <T> (ostream & out, const classType <T> & obj );

Friend istream & operator <(istream & in, classType & obj );
Template <class T>
Friend istream & operator <T> (istream & in, classType <T> & obj );

Friend ostream & operator <(ostream & out, const classType & obj );
Template <class T>
Friend ostream & operator <T> (ostream & out, const classType <T> & obj );
 
Friend istream & operator <(istream & in, classType & obj );
Template <class T>
Friend istream & operator <T> (istream & in, classType <T> & obj );


(2) Arithmetic Operators and Relational operators overload


[Cpp]
Friend classType operator + (constclassType & op1, const classType & op2 );

Friend classType operator + (constclassType & op1, const classType & op2 );


(3) value assignment operator overloading

The class value assignment operator accepts class type parameters. Generally, this parameter is a const reference to the class type, but it can also be a non-const reference. If this operator is not defined, the compiler will merge it. The class value assignment operator must be a member of the class so that the compiler specifies whether to synthesize one. You can define many additional value assignment operators for a class. The value assignment must return a reference to * this.


[Cpp]
String & operator = (const string &);
String & operator = (const char *);
String & operator = (char );

String & operator = (const string &);
String & operator = (const char *);
String & operator = (char );


(4) subscript operator overloading

The subscript operator must generate the left value when it appears on the left. You can specify the reference as the return type to obtain the left value. As long as the subscript operator returns a reference, you can assign a value to any party. It is also a good idea to use subscript for const and non-const objects. When a const object is applied, the return value is referenced by const, so it cannot be used as the value assignment target.


[Cpp]
Int & operator [] (const size_t index)
{Return data [index];}
Const int & operator [] (const size_tindex) const
{Return data [index];}

Int & operator [] (const size_t index)
{Return data [index];}
Const int & operator [] (const size_tindex) const
{Return data [index];}


(5) member access Operator Overloading

The arrow operator must be defined as a member function of the class. The unreferenced operator is not required to be defined as a member. The unreferenced and arrow operators are in the class that implements smart pointers.


[Cpp]
Class screen {
Public:
Screen & operator * () {return * ptr-> sp ;}
Screen * operator-> () {return ptr-> sp ;}
Const screen & operator * () const
{Return * ptr-> sp ;}
Const screen * operator-> () const
{Return ptr-> sp ;}
Private:
ScrPtr * ptr;
};

Class screen {
Public:
Screen & operator * () {return * ptr-> sp ;}
Screen * operator-> () {return ptr-> sp ;}
Const screen & operator * () const
{Return * ptr-> sp ;}
Const screen * operator-> () const
{Return ptr-> sp ;}
Private:
ScrPtr * ptr;
};
The overload arrow operator must return a pointer to the class type, or return a class object that defines its own arrow operator. If the return type is pointer, the pointer is used directly. If the returned result is an object that defines the arrow operator, the arrow operator of the returned object is recursively called. If the returned object does not define the arrow operator, the compiler reports an error.

 


(6) ++ Operator Overloading


[Cpp]
ClassType & operator ++ (); // prefix
ClassType & operator ++ (int); // suffix
ClassType & operator ++ (int)
{
ClassType ret (* this );
+ + * This; // call the prefix
Return ret;
}
ClassType obj;
Obj. operator ++ (); // display the call prefix
Obj. operator ++ (0); // display the call suffix

ClassType & operator ++ (); // prefix
ClassType & operator ++ (int); // suffix
ClassType & operator ++ (int)
{
ClassType ret (* this );
+ + * This; // call the prefix
Return ret;
}
ClassType obj;
Obj. operator ++ (); // display the call prefix
Obj. operator ++ (0); // display the call suffix
 

(7) const-based overload (exclusive to the class)

A const object can only use a const member. A non-const member can use any member, but a non-const version is a better match.


[Cpp]
Class Screen {
Public:
Screen & display (ostream & out)
{}
Const screen & display (ostream & out) const
{}
};
Screen myScreen (5, 3 );
Const Screen blank (5, 3 );
MyScreen. set ('#'). display (cout); // call the non-const version of display.
Blank. display (cout); // call the const version of display.

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.