Think_in_CPP第十二章 操作符重载(4)

本文聚焦于C++中操作符重载的应用,详细讨论了操作符[ ]和->的重载。通过SmartPointer类实例,解释了如何实现->重载以返回Obj指针,使得能直接调用对象的成员函数。同时,提到了操作符[ ]的重载在某些场景下的使用,并给出了测试相关的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

12.5 其他操作符

12.5.1 操作符[ ]

class IntArray { 
  enum { sz = 5 }; 
  int i[sz]; 
public: 
  IntArray() { memset(i, 0, sz* sizeof(*i)); } 
  int& operator[](int x) { 
    require(x >= 0 && x < sz, 
      "IntArray::operator[] out of range"); 
    return i[x]; 
  } 
  friend ostream& 
    operator<<(ostream& os, const IntArray& ia); 
  friend istream& 
    operator>>(istream& is, IntArray& ia); 
}; 

ostream&
operator<<(ostream& os, const IntArray& ia) {
  for(int j = 0; j < ia.sz; j++) {
     os << ia.i[j];
     if(j != ia.sz -1)
        os << ", ";
  }
  os << endl;
  return os;
}
 
istream& 
operator>>(istream& is, IntArray& ia){
  for(int j = 0; j < ia.sz; j++)
    is >> ia.i[j];
  return is;
}

测试

int main() { 
  stringstream input("47 34 56 92 103"); 
  IntArray I; 
  input >> I; 
  I[4] = -1; // Use overloaded operator[] 
  cout << I; 
} 

12.5.2 new和delete

参考13章

12.5.3 逗号重载

#include <iostream> 
using namespace std; 
class After { 
public: 
   const After& operator,(const After&) const { 
     cout << "After::operator,()" << endl; 
     return *this; 
   } 
}; 

class Before {}; 
Before& operator,(int, Before& b) { 
   cout << "Before::operator,()" << endl; 
   return b; 
} 
测试

int main() { 
   After a, b; 
   a, b; // Operator comma called 
   Before c; 
   1, c; // Operator comma called 
} 

12.5.4 操作符->

#include <iostream> 
#include <vector> 
#include "../require.h" 
using namespace std; 
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 = 47; 
int Obj::j = 11; 
// Container: 
class ObjContainer { 
   vector<Obj*> a; 
public: 
   void add(Obj* obj) { a.push_back(obj); } 
   friend class SmartPointer; 
}; 
class SmartPointer { 
   ObjContainer& oc; 
   int index; 
public: 
   SmartPointer(ObjContainer& objc) : oc(objc) { 
     index = 0; 
   } 
   // Return value indicates end of list: 
   bool operator++() { // Prefix 
     if(index >= oc.a.size()) return false; 
     if(oc.a[++index] == 0) return false; 
     return true; 
   } 
   bool operator++(int) { // Postfix 
     return operator++(); // Use prefix version 
   } 
   Obj* operator->() const { 
     require(oc.a[index] != 0, "Zero value " 
        "returned by SmartPointer::operator->()"); 
     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]); // Fill it up 
   SmartPointer sp(oc); // Create an iterator 
   do { 
     sp->f(); // Pointer dereference operator call 
     sp->g(); 
   } while(sp++); 
} 

说明:

1.SmartPointer重载了->,其行为是返回一个Obj指针。sp->f()执行的结果是调用某个Obj对象的f()成员函数。

2.->重载函数应该返回一个指针。


a nested iterator

#include <iostream>
#include <vector>
#include "../require.h"
using namespace std;
class Obj {
  static int i, j;
public:
  void f() { cout << i++ << endl; }
  void g() { cout << j++ << endl; }
};
// Static member definitions:
int Obj::i = 47;
int Obj::j = 11;

// Container:
class ObjContainer {
  vector<Obj*> a;
public:
  void add(Obj* obj) { a.push_back(obj); }
  class SmartPointer;
  friend SmartPointer;

  class SmartPointer {
    ObjContainer& oc;
    unsigned int index;
  public:
    SmartPointer(ObjContainer& objc) : oc(objc) {
      index = 0;
    }
  // Return value indicates end of list:
    bool operator++() { // Prefix
      if(index >= oc.a.size()) return false;
      if(oc.a[++index] == 0) return false;
      return true;
    }
    bool operator++(int) { // Postfix
      return operator++(); // Use prefix version
    }
    Obj* operator->() const {
      require(oc.a[index] != 0, "Zero value returned by SmartPointer::operator->()");
      return oc.a[index];
    }
  };
// Function to produce a smart pointer that
// points to the beginning of the ObjContainer:
  SmartPointer begin() {
    return SmartPointer(*this);
  }
};
 

int main() {
  const int sz = 10;
  Obj o[sz];
  ObjContainer oc;
  for(int i = 0; i < sz; i++)
    oc.add(&o[i]); // Fill it up
  ObjContainer::SmartPointer sp = oc.begin();
  do {
    sp->f(); // Pointer dereference operator call
    sp->g();
  } while(++sp);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值