C++ 类成员访问运算符重载
类成员访问运算符 (->) 可以重载,但操作起来比较复杂。它被定义为赋予类类型"类似指针"的行为。运算符 -> 必须是成员函数。如果使用,其返回类型必须是指针或可应用的类的对象。
运算符 -> 通常与指针解引用运算符 * 结合使用,以实现"智能指针"。这些指针是行为类似于普通指针的对象,但通过它们访问对象时会执行其他任务,例如在指针被销毁或指针用于指向另一个对象时自动删除对象。
解引用运算符 -> 可以定义为一元后缀运算符。也就是说,给定一个类 -
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;
// 考虑一个实际的类。
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};
// 静态成员定义:
int Obj::i = 10;
int Obj::j = 12;
// 为上述类实现一个容器
class ObjContainer {
vector<Obj*> a;
public:
void add(Obj* obj) {
a.push_back(obj); // 调用向量的标准方法。
}
friend class SmartPointer;
};
// 实现智能指针来访问 Obj 类的成员。
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc) {
oc = objc;
index = 0;
}
// 返回值表示列表的结尾:
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++();
}
// 重载 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); // 创建一个迭代器
do {
sp->f(); // 智能指针调用
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

cpp_overloading.html