The C ++ operator is overloaded to display the Conversion Relationship Between calls and implicit calls.

Source: Internet
Author: User
Tags gety

Operator overload
I. meaning and definition of operator overloading

L The existing operators of C ++ are only suitable for processing the basic data types of C ++.

L c ++ allows you to redefine existing operators (Operator overloading) so that it can process programmer-defined types (class types ).

L Operator Overloading is to assign multiple meanings to existing operators. Operator Overloading is similar to function overloading, which is a special type.

L c ++ re-defines the operator so that it can be used for objects of specific classes to execute specific functions.

L through the redefinition of the +,-, *,/operator, they can complete the addition, subtraction, multiplication, division operations for objects of different classes, such as the complex number and score. Enhanced the scalability of the C ++ language.

L create an operator function, which is generally defined as a member function or a friend function of the class.

 

Ii. Reload an operator principle:

1. The initial meaning of the operator cannot be changed. (Just a specification)

2. The number of parameters of the operator cannot be changed. For example, it is incorrect to use only one operand when reload operator + is used.

3. operator functions cannot include default parameters.

4. Most C ++ operators can be reloaded, except for the following:

.::.*?

5. Except the value assignment operator, other operator functions can be inherited by the derived class.

6. The operator reload does not change the operator priority and combination, nor the operator syntax structure. That is, the single object and binary operators can only be reloaded as single object and binary operators.

7. the overload of operators is actually the overload of functions. The compiler selects Operator Overloading according to the selection principle of function overloading. When there are not obvious operators, the compiler will look for operator functions with matching parameters.

8. Operator Overloading makes the program more concise, makes the expression more intuitive, and enhances readability. However, it is not recommended to use too much.

9. The meaning of the overload operator must be clear:

For example, a time class has three data members: hour, minute, and second.

Calss time {

Public:

Time () {hours = minutes = seconds = 0 ;}

Time (int h, int M, int s) {hours = H; minutes = m; seconds = s ;}

PRIVATE:

Int hours, minutes, seconds;

};

Time T1 (8, 10, 20), T2 (9, 15, 30), T3;

T3 = t1 + T2;

Here, the addition (+) operation is used for objects of the time class, And the meaning is unclear. Therefore, the overload operator + cannot be defined for the class time.

 

Iii. Two forms of operator overload functions: (mainly the interpretation principles of the C ++ compiler, useful)

Both member function form and friend function form can be private members in the member class.

1. Reload as a member function of the class

1) Reload the unary operator @ in Class X @

Return Type X: Operator @()

{... }

No parameter is specified, because it already has an implicit this parameter, for an object OBJ of the X class:

Explanation of expression C ++ Compiler

@ OBJ operator @ (OBJ)

OBJ @ operator @ (OBJ, 0)

2) overload binary operators in Class X @

Return Type X: Operator @ (parameter description)

{... }

Because the class member function has one this parameter, only one parameter can be specified at this time, for the OBJ object:

Explanation of expression C ++ Compiler

Obj1 @ obj2 obj1 · operator @ (obj2)

 

 

For example, four operators that are overloaded by the plural operator. The plural is constructed by the real part and the virtual part. It defines a plural class and then reloads the operators of the four arithmetic operations of the plural in the class.

 

# Include <iostream. h>

Class Complex

{PRIVATE:

Float real, imag;

Public:

Complex (float r = 0, float I = 0 );

Complex operator + (complex & C );

Complex operator-(complex & C );

Friend void print (complex & C );

};

Complex: complex (float R, float I)

{Real = r; imag = I ;}

Complex complex: Operator + (complex & C)

{Float r = real + C. Real; float I = imag + C. imag;

Return complex (R, I );}

Complex complex: Operator-(complex & C)

{Float r = real-c.real; float I = imag-c.imag;

Return complex (R, I );}

Void print (complex & C)

{Cout <'(' <C. Real <',' <C. imag <')' <Endl ;}

Void main ()

{Complex C1 (2.5, 3.7), C2 (4.2, 6.5 );

Complex C;

C = c1-c2; // C = C1 · operator-(C2)

Print (C );

C = C1 + C2; // C = C1 · operator + (C2)

Print (C );

}

 

This program defines a complex class and defines two member functions as operators to overload functions.

The C1 + C2 Compilation Program is interpreted as: c1.operator + (C2)

C1 and c2 are complex class objects. Operator + () is an operator + overload function.

This operator overload function has only one parameter C2. When the overload is a member function, the binary operator has only one parameter.

When a single-object operator is overloaded as a member function, parameters cannot be explicitly described.

When the overload is a member function, a parameter is always hidden, that is, the this pointer, which is a pointer to call the member function object.

 

In heavy-load operator functions, parameters in operator + () are passed by reference instead of pointers.

Because pointer passing has a program readability problem. For example, the operator overload declaration is:

Complex operator + (complex * C );

When called

Complex C1 (2.0, 3.0), C2 (4.0,-2.0), C3;

C3 = & C1 + & C2; it is considered that the address of C1 is added to the address of C2.

 

Complex operator + (const complex & C );

Then C3 = C1 + C2;

 

2. the overload is a friend function.

The operator overload function can also be a friend function. When the overload is a friend function, there is no implicit parameter This pointer. In this way, for Binary operators, youyuan function has two parameters. For single-object operators, youyuan function has one parameter. However, some operators cannot be reloaded as friend functions. They are: =, (), [], and->.

 

1) Reload the unary operator @ in Class X @

Return type operator @ (X & OBJ)

{... }

Only one parameter can be specified for the youyuan operator. For an object of the X class, OBJ:

Explanation of expression C ++ Compiler

@ OBJ operator @ (OBJ)

OBJ @ operator @ (OBJ, 0)

2) overload binary operators in Class X @

Return type operator @ (parameter description 1, parameter description 2)

{……}]

At least one of the two parameters must be of the X type, with the object obj1, obj2

Explanation of expression C ++ Compiler

Obj1 @ obj2 operator @ (obj1, obj2)

 

 

 

For example, replace the member function to compile the above program:

# Include <iostream. h>

Class Complex

{PRIVATE:

Float real, imag;

Public:

Complex (float r = 0, float I = 0 );

Friend complex operator + (complex & C1, complex & C2 );

Friend complex operator-(complex & C1, complex & C2 );

Friend void print (complex & C );

};

Complex: complex (float R, float I)

{Real = r; imag = I ;}

Complex operator + (complex & C1, complex & C2)

{Float r = c1.real + c2.real; float I = c1.imag + c2.imag;

Return complex (R, I );}

Complex operator-(complex & C1, complex & C2)

{Float r = c1.real-c2.real; float I = c1.imag-c2.imag;

Return complex (R, I );}

Void print (complex & C)

{Cout <'(' <C. Real <',' <C. imag <')' <Endl ;}

Void main ()

{

Complex C1 (2.5, 3.7), C2 (4.2, 6.5 );

Complex C;

C = c1-c2; // C = C1 · operator-(C2)

Print (C );

C = C1 + C2; // C = C1 · operator + (C2)

Print (C );

}

 

When binary operators are overloaded as member functions, there is only one parameter, and the other is hidden;

When the overload is a friend function, there are two parameters without implicit parameters;

C1 + C2: Operator + (C1, C2)

Call the following function to evaluate the value,

Complex operator + (const complex & C1, consrt complex & C2)

Conclusion 1: For binary operators, it is easier to overload a friend function than to overload a member function. As a friend function, binary operators do not require the first parameter to be a certain type of object.

Conclusion 2: For The unary operator, reload it into a member function is the most appropriate. You can also use the overload function as a Member.

 

Example:

# Include <iostream. h>

Class {

Public:

A () {x = y = 0 ;}

A (int I, Int J) {x = I; y = J ;}

A (A & P) {x = p. x; y = P. Y ;}

A & operator = (A & P );

Int getx () {return X ;}

Int Gety () {return y ;}

PRIVATE:

 

Int X, Y;

};

A & A: Operator = (A & P)

{

X = p. x;

Y = P. Y;

Cout <"assignment operator called. \ n ";

Return * this;

}

Void main ()

{

A A (7, 8 );

A B;

B =;

Cout <B. getx () <"," <B. Gety () <Endl;

}

 

Assignment operator called.

7, 8

This program defines a value assignment operator function in Class A and is defined as a member function.

B = A: B. Operator = ()

Call the following function: A & A: Operator = (A & P) to complete the assignment operation.

 

 

 

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.