Home  >  Article  >  Backend Development  >  Summary of C++ Review Key Points No. 7 - Operator Overloading

Summary of C++ Review Key Points No. 7 - Operator Overloading

黄舟
黄舟Original
2017-01-16 11:37:531347browse

Operator overloading
The so-called overloading is to give new meaning. Function overloading is to give an existing function a new meaning and enable it to implement new functions. Therefore, one function name can be used to represent functions with different functions, that is, "one name has multiple uses."

Operators can also be overloaded. In fact, we are already using operator overloading without even realizing it. For example, everyone is accustomed to using the addition operator "+" to add integers, single-precision numbers, and double-precision numbers, such as 5+8, 5.8 +3.67, etc. In fact, computers can add integers, single-precision numbers, and double-precision numbers. The addition operation process is very different, but since C++ has overloaded the operator "+", it can be applied to operations of int, float, and doUble types.

The operands of predefined operators in C++ can only be basic data types. But in fact, similar operations are also required for many user-defined types (such as classes). At this time, these operators must be redefined in C++ and given new functions to existing operators so that they can be used for specific types to perform specific operations. The essence of operator overloading is function overloading, which provides the scalability of C++ and is one of the most attractive features of C++.

Operator overloading is achieved by creating an operator function, which defines the operation that the overloaded operator will perform. The definition of an operator function is similar to the definition of other functions. The only difference is that the function name of the operator function is composed of the keyword operator and the operator symbol to be overloaded. The general format of operator function definition is as follows:

operator ()

{
     <函数体>
}

When operator is overloaded Follow the following rules:

(1) Except for the generic relation operator ".", member pointer operator ".*", scope operator "::", sizeof operator and ternary operator" Except for ?:", all operators in C++ can be overloaded.

(2) Overloaded operators are limited to operators that allow overloading within the scope of existing operators in the C++ language, and new operators cannot be created.

(3) Operator overloading is essentially function overloading, so the compiler’s choice of operator overloading follows the selection principle of function overloading.

(4) Overloaded operators cannot change the priority and associativity of the operator, nor can they change the number of operands and the syntax structure of the operator.

(5) Operator overloading cannot change the meaning of the operator for internal type objects. It can only be used with objects of user-defined types, or when objects of user-defined types and objects of internal types are mixed.

(6) Operator overloading is an appropriate transformation of the original operator according to the actual needs of new types of data. The overloaded function should be similar to the original function to avoid using overloaded operations without purpose. symbol.

Operator function overloading generally has two forms: overloading as a member function of the class and overloading as a non-member function of the class. Non-member functions are usually friends. (An operator can be overloaded as a non-member, non-friend function. However, when such an operator function accesses the private and protected members of the class, it must use the methods of setting data and reading data provided in the public interface of the class. Functions, performance will be reduced when calling these functions. These functions can be inlined to improve performance)

Member function operator

The general format of operator overloading as a member function of a class is:

  <函数类型> operator <运算符>(<参数表>)
    {
     <函数体>
    }

When an operator is overloaded as a member function of a class, the number of parameters of the function is one less than the original operand (except for the post-fixed unary operator). This is because the member function uses The this pointer implicitly accesses an object of the class and serves as the leftmost operand of the operator function. Therefore:

(1) When the binary operator is overloaded as a member function of the class, the function only explicitly states one parameter, which is the right operand of the operator.

(2) When the prefixed unary operator is overloaded as a member function of a class, there is no need to explicitly state the parameters, that is, the function has no formal parameters.

(3) When the post-fixed unary operator is overloaded as a member function of the class, the function must have an integer parameter.

The format for calling member function operators is as follows:

    <对象名>.operator <运算符>(<参数>)

It is equivalent to

    <对象名><运算符><参数>

For example: a+b Equivalent to a.operator +(b). In general, we use the idiomatic expression of operators.

Friend function operator

The general format of the operator overloaded as a friend function of the class is:

    friend <函数类型> operator <运算符>(<参数表>)
    {
     <函数体>
    }

When the operator is overloaded as When a class is a friend function, since there is no implicit this pointer, the number of operands does not change. All operands must be passed through the formal parameters of the function. The parameters and operands of the function are passed from left to right. correspond.

The format for calling the friend function operator is as follows:

  operator <运算符>(<参数1>,<参数2>)

It is equivalent to

    <参数1><运算符><参数2>

For example: a+ b is equivalent to operator +(a,b).

Comparison of two overloaded forms

  在多数情况下,将运算符重载为类的成员函数和类的友元函数都是可以的。但成员函数运算符与友元函数运算符也具有各自的一些特点:

(1) 一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。

(2) 以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。

(3) 类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。

(4) 若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。

(5) 若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。

(6) 当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一 个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部 类型的对象,该运算符函数必须作为一个友元函数来实现。

(7) 当需要重载运算符具有可交换性时,选择重载为友元函数。

1 为什么会用运算符重载机制(回忆函数重载)

         用复数类举例    //Complex c3 =c1 + c2;

         //原因 Complex是用户自定义类型,编译器根本不知道如何进行加减

         //编译器给提供了一种机制,让用户自己去完成,自定义类型的加减操作。。。。。

         //这个机制就是运算符重载机制

2 运算符重载的本质是一个函数

Summary of C++ Review Key Points No. 7 - Operator Overloading


Summary of C++ Review Key Points No. 7 - Operator Overloading

Summary of C++ Review Key Points No. 7 - Operator Overloading

例如1:

//通过类成员函数完成 - 操作符重载(函数的定义在类里面)
//函数声明 Complex operator-(Complex &c2)
//用类成员函数实现-运算符重载
Complex c4 = c1 - c2; 
c4.printCom(); //打印输出
//c1.operator-(c2);

例如2:

//通过全局函数方法完成+操作符重载(函数的定义在类外)
//函数声明 Complex operator+(Complex &c1, Complex &c2)
//函数调用分析
int main()
{ Complex c1(1, 2), c2(3, 4);//定义两个对象
//Complex c31 = operator+(c1,c2); 函数的调用模式
Complex c3 = c1 + c2; //简写+
c3.printCom();
}

Summary of C++ Review Key Points No. 7 - Operator Overloading

感受操作过程:

例如3:

//前置++操作符 用全局函数实现
Complex& operator++(Complex&c1) 
{ c1.a ++; 
c1.b ++;
return c1; 
}
//调用方法
++c1 ; //=è需要写出操作符重载函数原形
c1.printCom();
//运算符重载函数名定义
//首先承认操作符重载是一个函数 定义函数名--->operator++
//分析函数参数 根据左右操作数的个数------->operator++(Complex &c1)
//分析函数返回值-------> Complex&operator++(Complex &c1) 返回它自身

例如4

//4.1 前置—操作符成员函数实现
Complex& operator--()
{ this->a--;
this->b--;
return *this; //注意返回
}
//4.2 调用方法
--c1;
c1.printCom();
//4.3前置—运算符重载函数名定义
//c1.operator--()

例如5:

//5.1//后置++ 操作符用全局函数实现
Complex operator++(Complex &c1, int) 
{ Complex tmp = c1;
c1.a++;
c1.b++;
return tmp;
}
//5.2 调用方法
c1 ++ ; //先使用后++
//5.3 后置++运算符重载函数名定义
Complex operator++(Complex&c1, int) //函数占位参数 和 前置++ 相区别

例如6:

//6.1 后置— 操作符 用类成员函数实现
Complexoperator--(int) 
{ Complextmp = *this;
this->a--;
this->b--;
return tmp;
}
//6.2 调用方法
c1 ++ ; //先使用后++
//6.3 后置--运算符重载函数名定义
Complex operator--(int) //函数占位参数和 前置-- 相区别

前置和后置运算符总结

C++中通过一个占位参数来区分前置运算和后置运算

定义运算符重载函数名的步骤

全局函数、类成员函数方法实现运算符重载步骤

1)要承认操作符重载是一个函数,写出函数名称operator+ () 

2)根据操作数,写出函数参数 

3)根据业务,完善函数返回值(看函数是返回引用 还是指针 元素),及实现函数业务

友元函数实现操作符重载的应用场景

1)友元函数和成员函数选择方法 
Ø 当无法修改左操作数的类时,使用全局函数进行重载
Ø =, [], ()和->操作符只能通过成员函数进行重载
2)用友元函数重载 << >>操作符
Ø istream 和 ostream 是 C++ 的预定义流类 
Ø cin 是 istream 的对象,cout 是 ostream 的对象
Ø 运算符 << 由ostream 重载为插入操作,用于输出基本类型数据
Ø 运算符 >> 由 istream 重载为提取操作,用于输入基本类型数据
Ø 用友员函数重载 << 和 >> ,输出和输入用户自定义的数据类型
a)用全局函数方法实现 << 操作符 
ostream& operator<<(ostream &out, Complex &c1)
{ //out<<"12345,生活真是苦"<<endl;
out<<c1.a<<"+ "<<c1.b<<"i "<<endl;
return out;
}
//调用方法
cout<<c1;
//链式编程支持
cout<<c1<<"abcc";
//cout.operator<<(c1).operator<<("abcd");
//函数返回值充当左值 需要返回一个引用
b)类成员函数方法无法实现 <<操作符重载
//因拿到cout这个类的源码
//cout.operator<<(c1);
3) 友元函数重载操作符使用注意点
a) 友员函数重载运算符常用于运算符的左右操作数类型不同的情况
b)其他
Ø 在第一个参数需要隐式转换的情形下,使用友员函数重载运算符是正确的选择
Ø 友员函数没有 this 指针,所需操作数都必须在参数表显式声明,很容易实现类型的隐式转换
Ø C++中不能用友员函数重载的运算符有
= () [] ->

请问:为什么运算法重载成员函数只有一个参数,而友元函数有两个?

由于成员函数有指向对象的this指针,可以通过对象传递参数;而友元函数没有this指针,所以必须要两个参数。(针对二元操作符重载而言)

以上就是c++复习要点总结之七——运算符重载的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn