目录

Class Member Access Operator -> 重载

类成员访问运算符( - >)可以重载但有点棘手。 它被定义为给类类型一个“指针式”行为。 运算符 - >必须是成员函数。 如果使用,则其返回类型必须是可以应用的类的指针或对象。

operator->经常与指针解引用运算符*一起使用,以实现“智能指针”。 这些指针是行为类似于普通指针的对象,除了它们通过它们访问对象时执行其他任务,例如在指针被销毁时指针被自动删除,或者指针用于指向另一个对象。

解除引用运算符 - >可以定义为一元后缀运算符。 也就是说,给一个class -

class Ptr {
   //...
   X * operator->();
};

Ptr对象可以用于以与指针使用方式非常类似的方式访问类X成员。 例如 -

void f(Ptr p ) {
   p->m = 10 ; // (p.operator->())->m = 10
}

语句p-> m被解释为(p.operator - >()) - > m。 使用相同的概念,下面的示例解释了如何重载类访问运算符 - >。

#include <iostream>
#include <vector>
using namespace std;
// Consider an actual class.
class Obj {
   static int i, j;
public:
   void f() const { cout << i++ << endl; }
   void g() const { cout << j++ << endl; }
};
// Static member definitions:
int Obj::i = 10;
int Obj::j = 12;
// Implement a container for the above class
class ObjContainer {
   vector<Obj*> a;
   public:
      void add(Obj* obj) { 
         a.push_back(obj);  // call vector's standard method.
      }
      friend class SmartPointer;
};
// implement smart pointer to access member of Obj class.
class SmartPointer {
   ObjContainer oc;
   int index;
   public:
      SmartPointer(ObjContainer& objc) { 
         oc = objc;
         index = 0;
      }
      // Return value indicates end of list:
      bool operator++() { // Prefix version 
         if(index >= oc.a.size()) return false;
         if(oc.a[++index] == 0) return false;
         return true;
      }
      bool operator++(int) { // Postfix version 
         return operator++();
      }
      // overload operator->
      Obj* operator->() const {
         if(!oc.a[index]) {
            cout << "Zero value";
            return (Obj*)0;
         }
         return oc.a[index];
      }
};
int main() {
   const int sz = 10;
   Obj o[sz];
   ObjContainer oc;
   for(int i = 0; i < sz; i++) {
      oc.add(&o[i]);
   }
   SmartPointer sp(oc); // Create an iterator
   do {
      sp->f(); // smart pointer call
      sp->g();
   } while(sp++);
   return 0;
}

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

10
12
11
13
12
14
13
15
14
16
15
17
16
18
17
19
18
20
19
21
↑回到顶部↑
WIKI教程 @2018