Think_in_CPP第十二章 操作符重载(4)
2012-09-15 20:44
239 查看
12.5 其他操作符
12.5.1 操作符[ ]
测试
12.5.2 new和delete
参考13章
12.5.3 逗号重载
12.5.4 操作符->
说明:
1.SmartPointer重载了->,其行为是返回一个Obj指针。sp->f()执行的结果是调用某个Obj对象的f()成员函数。
2.->重载函数应该返回一个指针。
a nested iterator
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); }
相关文章推荐
- Think_in_CPP第十二章 操作符重载(5)
- Think_in_CPP第十二章 操作符重载(6)
- Think_in_CPP第十二章 操作符重载(7)
- Think_in_CPP第十二章 操作符重载(3)
- Think_in_CPP第十二章 操作符重载(1)
- Think_in_CPP第十二章 操作符重载(2)
- Think_in_CPP第十三章 动态建立对象(1)
- think in java第十二章笔记
- Think in C++ error StreamTokenizer.cpp
- think in java第十二章读书笔记
- think in google
- Think In Java Chapter 18 IO系统 练习17 计算文件中字符出现次数
- 浅谈《think in java》:二 一切都是对象
- Think in Java
- Think before you code, Virtual Functions in C++
- Think in Java
- Think in java, notes
- 编译 apache-log4cxx-0.10.0inputstreamreader.cpp:66: error: ‘memmove’ was not declared in this sco
- think in java_String
- think in java interview-高级开发人员面试宝典(十)