哪位帮忙解决下问题
//: C12:SmartPointer.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#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++);
} ///:~
在main中的
sp-> f(); // Pointer dereference operator call
sp-> g();
我不是很明白
sp-> 返回的是Obj * 那么想调用 f()或g()不是还要再加上-> 吗 ?
例如sp-> -> f();
[解决办法]
SmartPointer的-> 被重载了, -> 返回的是Obj的指针,所以 s-> 直接应用Obj类的内容