code:
/* 第14章 重载操作符与转换 14.1 重载操作符的定义 14.2 输入和输出操作符 14.3 算术操作符和关系操作符 14.4 赋值操作符 14.5 下标操作符 14.6 成员访问操作符 14.7 自增操作符和自减操作符 14.8 调用操作符和函数对象 14.9 转换与类类型 小结 第14章 重载操作符与转换 429 14.1 重载操作符的定义 430 14.2 输入和输出操作符 435 14.2.1 输出操作符<<的重载 435 14.2.2 输入操作符>>的重载 437 14.3 算术操作符和关系操作符 439 14.3.1 相等操作符 440 14.3.2 关系操作符 441 14.4 赋值操作符 441 14.5 下标操作符 442 14.6 成员访问操作符 443 14.7 自增操作符和自减操作符 446 14.8 调用操作符和函数对象 449 14.8.1 将函数对象用于标准库算法 450 14.8.2 标准库定义的函数对象 451 14.8.3 函数对象的函数适配器 453 14.9 转换与类类型 454 14.9.1 转换为什么有用 454 14.9.2 转换操作符 455 14.9.3 实参匹配和转换 458 14.9.4 重载确定和类的实参 461 14.9.5 重载、转换和操作符 464 小结 466 术语 467 */ // 14.1 重载操作符的定义 ------------------------------------------------------------------------------------------- #include <iostream> #include <string> using namespace std; class test { int i; public: test& operator+=(const test&); test():i(0){} test(int j):i(j){} int get(){return i;} }; test& test::operator+=(const test& t) { i+=t.i; return *this; } int main() { class test ta(2),tb(3); //ta += tb; ta.operator+=(tb); cout << ta.get() << endl; return 0; } // 14.2 输入和输出操作符 ------------------------------------------------------------------------------------------- // 14.3 算术操作符和关系操作符 ------------------------------------------------------------------------------------------- #include <iostream> #include <string> using namespace std; class test { int i; public: test& operator+=(const test&); test():i(0){} test(int j):i(j){} int get(){return i;} }; test& test::operator+=(const test& t) { i+=t.i; return *this; } test operator+(const test& lhs, const test& rhs) { test ret(lhs); ret += rhs; return ret; } int main() { class test ta(2),tb(3); class test tc; tc = ta+tb; cout << tc.get() << endl; return 0; } // 14.4 赋值操作符 ------------------------------------------------------------------------------------------- #include <iostream> #include <string> using namespace std; class test { int i; public: test& operator+=(const test&); test& operator=(const int&); // = int test& operator=(const test&); // = class test():i(0){} test(int j):i(j){} int get(){return i;} }; test& test::operator=(const int& j) { i=j; return *this; } test& test::operator=(const test& t) { i=t.i; return *this; } test& test::operator+=(const test& t) { i+=t.i; return *this; } test operator+(const test& lhs, const test& rhs) { test ret(lhs); ret += rhs; return ret; } int main() { class test ta(2),tb(3); class test tc; tc = 5; cout << tc.get() << endl; tc = ta; cout << tc.get() << endl; tc = tb; cout << tc.get() << endl; return 0; } // 14.5 下标操作符 ------------------------------------------------------------------------------------------- #include <iostream> #include <string> using namespace std; class test { int i; public: int operator[](const int j){ return(i+j);} const int operator[](const int) const; test():i(0){} test(int j):i(j){} int get(){return i;} }; const int test::operator[](const int j) const { return( j<0?0:i+j ); } int main() { class test ta(2),tb(3); cout << ta[2] << endl; cout << tb[-3] << endl; return 0; } // 14.6 成员访问操作符 ------------------------------------------------------------------------------------------- // 这节没细看 // 14.7 自增操作符和自减操作符 ------------------------------------------------------------------------------------------- // my test #include <iostream> #include <string> #include <stdexcept> using namespace std; int main() { //throw runtime_error("ISBN"); throw out_of_range ("decrement past the beginning of CheckedPtr"); return 0; } // ++obj #include <iostream> #include <string> #include <stdexcept> using namespace std; /* * smart pointer: Checks access to elements throws an out_of_range * exception if attempt to access a nonexistent element * users allocate and free the array */ class CheckedPtr { public: // no default constructor; CheckedPtrs must be bound to an object CheckedPtr(int *b, int *e): beg(b), end(e), curr(b){} // dereference and increment operations public: CheckedPtr &operator++(); // prefix operators CheckedPtr &operator--(); // other members as before private: int *beg; // pointer to beginning of the array int *end; // one past the end of the array int *curr; // current position within the array }; // prefix: return reference to incremented/decremented object CheckedPtr &CheckedPtr::operator++() { if(curr == end) throw out_of_range("increment past the end of CheckedPtr"); ++curr; // advance current state return *this; } CheckedPtr &CheckedPtr::operator--() { if(curr == beg) throw out_of_range("decrement past the beginning of CheckedPtr"); --curr; // move current state back one element return *this; } int main() { int a[]={2,3,5}; CheckedPtr cp(a,a+3); --cp; return 0; } // obj++ #include <iostream> #include <string> #include <stdexcept> using namespace std; /* * smart pointer: Checks access to elements throws an out_of_range * exception if attempt to access a nonexistent element * users allocate and free the array */ class CheckedPtr { public: // increment and decrement CheckedPtr operator++(int); // postfix operators CheckedPtr operator--(int); // 后缀--,比如 i-- //那个形参不是后缀式操作符的正常工作所需要的,它的唯一目的是使后缀函数与前缀函数区别开来。 // no default constructor; CheckedPtrs must be bound to an object CheckedPtr(int *b, int *e): beg(b), end(e), curr(b){} // dereference and increment operations public: CheckedPtr &operator++(); // prefix operators CheckedPtr &operator--(); // other members as before private: int *beg; // pointer to beginning of the array int *end; // one past the end of the array int *curr; // current position within the array }; // postfix: increment/decrement object but return unchanged value CheckedPtr CheckedPtr::operator++(int) { // no check needed here, the call to prefix increment will do the check CheckedPtr ret(*this); // save current value ++ *this; // advance one element, checking the increment return ret; // return saved state 先用后加 } CheckedPtr CheckedPtr::operator--(int) { // no check needed here, the call to prefix decrement will do the check CheckedPtr ret(*this); // save current value -- *this; // move backward one element and check return ret; // return saved state 先用后减 } // prefix: return reference to incremented/decremented object CheckedPtr &CheckedPtr::operator++() { if(curr == end) throw out_of_range("increment past the end of CheckedPtr"); ++curr; // advance current state return *this; } CheckedPtr &CheckedPtr::operator--() { if(curr == beg) throw out_of_range("decrement past the beginning of CheckedPtr"); --curr; // move current state back one element return *this; } int main() { int a[]={2,3,5}; CheckedPtr cp(a,a+3); //--cp; cp--; // 显式调用 cp.operator++(0); cp.operator++(); return 0; } // 14.8 调用操作符和函数对象 ------------------------------------------------------------------------------------------- // 函数对象 #include <iostream> #include <string> using namespace std; //定义了重载调用操作符的类的对象,叫函数对象 struct absInt { int operator()(int val) { return val < 0 ? - val: val; } }; int main() { int i = - 42; absInt absObj; // object that defines function call operator unsigned int ui = absObj(i); // calls absInt::operator(int) cout << ui << endl; return 0; } // 谓词函数,作为参数 #include <iostream> #include <string> #include <vector> using namespace std; // determine whether a length of a given word is 6 or more bool GT6(const string &s) { return s.size() >= 6; } int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); vector < string > ::size_type wc = count_if(v.begin(), v.end(), GT6); cout << wc << endl; return 0; } // 函数对象,作为谓词。 #include <iostream> #include <string> #include <vector> using namespace std; // determine whether a length of a given word is longer than a stored bound class GT_cls { public: GT_cls(size_t val = 0) : bound(val){} bool operator()(const string &s) { return s.size() >= bound; } private: string::size_type bound; }; int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); cout << count_if(v.begin(), v.end(), GT_cls(6)) // 重载了() << " words 6 characters or longer" << endl; return 0; } #include <iostream> #include <string> #include <vector> using namespace std; // determine whether a length of a given word is longer than a stored bound class GT_cls { public: GT_cls(size_t val = 0) : bound(val){} bool operator()(const string &s) { return s.size() >= bound; } private: string::size_type bound; }; int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); for(size_t i = 0; i != 11; ++i) cout << count_if(v.begin(), v.end(), GT_cls(i)) << " words " << i << " characters or longer" << endl; return 0; } // 14.8.2. 标准库定义的函数对象 #include <iostream> #include <string> #include <vector> #include <functional> using namespace std; int main() { plus < int > intAdd; // function object that can add two int values negate < int > intNegate; // function object that can negate an int value // uses intAdd::operator(int, int) to add 10 and 20 int sum = intAdd(10, 20); // sum = 30 函数对象重载了 () cout << sum << endl; // uses intNegate::operator(int) to generate -10 as second parameter // to intAdd::operator(int, int) sum = intAdd(10, intNegate(10)); // sum = 0 cout << sum << endl; // my test greater<int> gr; cout << gr(2,1) << endl; return 0; } // 谓词函数 #include <iostream> #include <string> #include <vector> #include <functional> using namespace std; int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); sort(v.begin(),v.end()); vector<string>::iterator it=v.begin(); while( it!=v.end() ) cout << *it++ << endl; sort(v.begin(),v.end(), greater<string>()); it=v.begin(); cout << endl; while( it!=v.end() ) cout << *it++ << endl; return 0; } // 14.8.3. 函数对象的函数适配器 #include <iostream> #include <string> #include <vector> #include <functional> using namespace std; int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); vector < string > ::size_type wc = count_if(v.begin(), v.end(), bind2nd(less_equal<string>(),"jjj") ); cout << wc << endl; return 0; } #include <iostream> #include <string> #include <vector> #include <functional> using namespace std; int main() { vector<string> v; v.push_back("abcdefg"); v.push_back("hijklmn"); v.push_back("test"); v.push_back("abc"); vector < string > ::size_type wc = count_if(v.begin(), v.end(), bind2nd(less_equal<string>(),"jjj") ); cout << wc << endl; wc=count_if(v.begin(),v.end(),not1(bind2nd(less_equal<string>(),"jjj"))); cout << wc << endl; return 0; } // 14.9 转换与类类型 ------------------------------------------------------------------------------------------- // in book #include <iostream> #include <string> #include <vector> #include <stdexcept> #include <functional> using namespace std; class SmallInt { public: SmallInt(int i = 0): val(i) { if(i < 0 || i > 255) throw std::out_of_range("Bad SmallInt initializer"); } operator int()const { return val; } // 将类类型,转换成int private: std::size_t val; }; int main() { //SmallInt si(-1); SmallInt si(3); cout<< si+4 << endl; si=3.14; cout << si << endl; return 0; }