C++ 重载(运算符和函数)



C++ 允许您在同一作用域中为函数名称或运算符指定多个定义,分别称为函数重载运算符重载

重载声明是在同一作用域中声明与先前声明的声明具有相同名称的声明,但这两个声明具有不同的参数,并且显然具有不同的定义(实现)。

当您调用重载函数运算符时,编译器会通过将您用来调用函数或运算符的参数类型与定义中指定的参数类型进行比较,来确定最合适的定义。选择最合适的重载函数或运算符的过程称为重载解析

C++ 中的函数重载

您可以在同一作用域中为同一个函数名称定义多个定义。函数的定义必须通过参数列表中参数的类型和/或数量相互区别。您不能重载仅返回值类型不同的函数声明。

阅读: C++ 函数重载

以下是使用相同的函数print()打印不同数据类型的示例:

#include <iostream>
using namespace std;
 
class printData {
   public:
      void print(int i) {
        cout << "Printing int: " << i << endl;
      }
      void print(double  f) {
        cout << "Printing float: " << f << endl;
      }
      void print(char* c) {
        cout << "Printing character: " << c << endl;
      }
};

int main(void) {
   printData pd;
 
   // Call print to print integer
   pd.print(5);
   
   // Call print to print float
   pd.print(500.263);
   
   // Call print to print character
   pd.print("Hello C++");
 
   return 0;
}

当以上代码编译并执行时,会产生以下结果:

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

C++ 中的运算符重载

您可以重新定义或重载 C++ 中可用的大多数内置运算符。因此,程序员也可以将运算符与用户定义的类型一起使用。

重载运算符是具有特殊名称的函数:“operator”关键字后跟要定义的运算符的符号。像任何其他函数一样,重载运算符具有返回类型和参数列表。

Box operator+(const Box&);

声明了可以用来添加两个 Box 对象并返回最终 Box 对象的加法运算符。大多数重载运算符可以定义为普通的非成员函数或类成员函数。如果我们将上述函数定义为类的非成员函数,则必须为每个操作数传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

以下示例演示了使用成员函数的概念来进行运算符重载。这里传递一个作为参数的对象,其属性将使用此对象进行访问,调用此运算符的对象可以使用this运算符进行访问,如下所述:

#include <iostream>
using namespace std;

class Box {
   public:
      double getVolume(void) {
         return length * breadth * height;
      }
      void setLength( double len ) {
         length = len;
      }
      void setBreadth( double bre ) {
         breadth = bre;
      }
      void setHeight( double hei ) {
         height = hei;
      }
      
      // Overload + operator to add two Box objects.
      Box operator+(const Box& b) {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
      
   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

// Main function for the program
int main() {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   Box Box3;                // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here
 
   // box 1 specification
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // box 2 specification
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // Add two object as follows:
   Box3 = Box1 + Box2;

   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

当以上代码编译并执行时,会产生以下结果:

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

可重载/不可重载运算符

以下是可重载的运算符列表:

+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []

以下是不可重载的运算符列表:

:: .* . ?:

运算符重载示例

以下是一些运算符重载示例,以帮助您理解该概念。

广告

© . All rights reserved.