C++ 运算符重载

C++ 中的运算符重载

您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

举个很简单的例子:

 // 重载 + 运算符,用于把两个 Box 对象相加
      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;
      }

下面列出操作符的有关类型:
操作符的重载

C++ 一元运算符重载

一元运算符只对一个操作数进行操作,下面是一元运算符的实例:

递增运算符( ++ )和递减运算符( -- )
一元减运算符,即负号( - )
逻辑非运算符( ! )

一元运算符通常出现在它们所操作的对象的左边,比如 !obj、-obj 和 ++obj,但有时它们也可以作为后缀,比如 obj++ 或 obj–。

Distance operator- ()  
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }

上面这个代码就构造了一个-操作符,让对象成员中的参量全部取负值。
然后返回一个新的Distance对象,这样可以让我们实现有关赋值。

C++ 二元运算符重载

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;
   }

返回的又是一个新定义的这个类的对象。

C++ 关系运算符重载

bool operator <(const Distance& d)
      {
         if(feet < d.feet)
         {
            return true;
         }
         if(feet == d.feet && inches < d.inches)
         {
            return true;
         }
         return false;
      }

这个是重载,比较的是这个类中的成员值,这种操作是你应该明确的。

C++ 输入/输出运算符重载

friend istream &operator>>( istream  &input, Distance &D )
      { 
         input >> D.feet >> D.inches;
         return input;            
      }

这个是重载输入字符,看这模版,这个需要两个操作数。

举个例子:

#include <iostream>

using namespace std;

class Myclass
{
   int a;
public:
   void print(){cout << a <<endl;}
   friend istream &operator>>(istream & input,Myclass &A)
   {
      input >> A.a;
      return input;
   }
};

int main()
{
   Myclass x;

   cin >> x;

   x.print();

   return 0;
}

注意,这个开头的关键字是friend,我们之后会更好的学习相关内容。

C++ ++ 和 – 运算符重载

 Time operator++ ()  
      {
         ++minutes;          // 对象加 1
         if(minutes >= 60)  
         {
            ++hours;
            minutes -= 60;
         }
         return Time(hours, minutes);
      }
      // 重载后缀递增运算符( ++ )
      Time operator++( int )         
      {
         // 保存原始值
         Time T(hours, minutes);
         // 对象加 1
         ++minutes;                    
         if(minutes >= 60)
         {
            ++hours;
            minutes -= 60;
         }
         // 返回旧的原始值
         return T; 
      }

注意,区分是使用前缀还是后缀,操作的主要区别是的重载的参数中是否有(int),如果没有,则为前缀,如果有,则为后缀。

#include <iostream>

using namespace std;

class Students
{
   int value;


public:
   Students operator++(int)
   {
      ++value;

      return Students(value);
   }

   Students(int a):value(a){}
   void content(){cout << value <<endl;}
};

int main()
{
   Students a(32);

   a++;
   a.content();
   return 0;
}

最后输出33,这个操作看看就明确的。

C++ 赋值运算符重载

像其他运算符一样,您可以重载赋值运算符( = ),用于创建一个对象,比如拷贝构造函数。

下面的实例演示了如何重载赋值运算符。

void operator=(const Distance &D )
      { 
         feet = D.feet;
         inches = D.inches;
      }

下面举个例子,结果是33,赋值操作自动加1。

#include <iostream>

using namespace std;

class Students
{
   int value;


public:
   void operator=(const Students &x)
   {
      value = x.value+1;
   }
  Students(){};

   Students(int a):value(a){}
   void content();
};

void Students::content()
{
   cout << value << endl;
}

int main()
{
   Students a(32);
   Students b;

   b=a;
   b.content();
   return 0;
}

C++ 函数调用运算符 () 重载

Distance operator()(int a, int b, int c)
      {
         Distance D;
         // 进行随机计算
         D.feet = a + c + 10;
         D.inches = b + c + 100 ;
         return D;
      }

D2 = D1(10, 10, 10); // invoke operator()

这个看起来很好理解。

C++ 下标运算符 [] 重载

int& operator[](int i)
      {
          if( i > SIZE )
          {
              cout << "索引超过最大值" <<endl; 
              // 返回第一个元素
              return arr[0];
          }
          return arr[i];
      }

返回其类中某个数组。

C++ 类成员访问运算符 -> 重载

类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予”指针”行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。

运算符 -> 通常与指针引用运算符 * 结合使用,用于实现”智能指针”的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。

这个可能涉及的有点多,我们现在还不太能够用到它,这是需要明确的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值