operator->重载问题总结 编写程序的时候,想让一个对象表现的像一个指针,因此需要重载->运算符。因此写下了如下的代码。由于全部贴出程序比较多,用简化的代码说明一下:
class A
{
public:
int i;
A(){i = 100;}
void print(int a){printf("%d/n", a);}
A& operator->(){return *this;}
};
int main()
{
int ret;
A a;
ret = a->i;
a->print(200);
}
发现编译不过去,想一想也对, A& operator->()这个按照正常的重载方法必须要有参数,比如a->i,则应为A& operator->(int i)。a->print(200),参数必须为函数。这又让我比较纠结了。考虑到这里想不"->"操作符的重载必然不是这种方法,但是我记得以前看过的书中又提到->重载的,却想不起来是那本书,在哪里。于是把c++ primer翻出来看了一下,没找到!又找了一下more effective c++也没找到。后来偶然在thinking in c++中看到了,虽然以前看过这本书,不过现在想来当时看的太不仔细了。不过由此也让我知道了->操作符重载与其他操作符重载不同的地方!
以下摘录于thinking in c++原文中部分描述。大概在280页的不常用的运算符章节
A pointer dereference operator must be a member function. It has additional, atypical constraints: It must return an object (or reference to an object) that also has a pointer dereference operator, or it must return a pointer that can be used to select what the pointer dereference operator arrow is pointing at. Here’s a simple example:
大致的意思就是重载->运算符一定是一个成员函数,它有额外的、非典型的限制:它必须返回一个对象(或对象的引用),该对象也有一个重载->运算符的函数;或者必须返回一个指针,被用于选择重载->箭头所指向的内容。
看完这段后,那么将我们的重载函数修改为A* operator->(){return this;}应该就可以完成需要的行为了。下面给一段thinking in c++中的例子:
#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++); } 使用ObjContainer的函数add()将指向这些对象的指针存储在类型为ObjContainer的容器中。ObjContainer看起来像一个指针数组,但却没有办法取回这些指针。然而,类SmartPointer被声明为友元类,所以它允许进入这个容器内。
在main()中,一旦Obj对象装入容器oc,一个SmartPointer类的SP就创建了,灵巧指针按下面的表达式进行调用:
sp->f(); // Pointer dereference operator call
sp->g(); 这里尽管sp实际上并没有成员函数f()和g(),但指针间接引用运算符自动的为用SmartPointer::operator->返回的Obj*调用那么函数。 原文地址:http://wsqhs.spaces./blog/cns!94F639580F58209C!346.entry |
|