此處不寫return type 由于已經指定double; 全局函數operator + (left + right) <–> left + right
注意此處與conversion function區別 如果定義d2 為double 類型與上面相同則無法轉換。 注意 operator + (單參數) <–> f + 4; 左 + 右 無法 調用 4 + f;
如果同時定義轉換函數和單參數non-explicit 構造函數將會造成錯誤編譯器無法選擇。 如果構造函數前面加上explicit 關鍵字 構造函數無法自動轉化4 為 Fraction 類型 此處也會產生錯誤。 STL中conversion函數的應用
template <class Alloc>class vector<bool,Alloc>{public:typedef __bit_reference refernce;protected:refernce operator[](size_type n) { return *(begin() + difference_type(n));}}...struct __bit_refernce { unsigned int*p; unsigned int mask;public: operator bool() const { return !(!(*p & mask));}}此處reference 可以自動轉換為bool 類型 。
1.智能指針
template<class T>class shared_ptr{public: T& operator*() const {return *px;} T* operator->() const {return px;} shared_ptr(T* p):px(p) {}private: T* px; long* pn;...};struct Foo{ method() {}};shared_ptr<Foo> sp(new Foo);Foo f(*sp);sp->method(); --> px->method即必須重載 類內操作符 operator * 和operator-> 注意此處-> 符號并不會消耗。 STL boost unique_ptr scoped_ptr 獨占指針對象,并保證指針所指對象生命周期與其一致 shared_ptr shared_ptr 可共享指針對象,可以賦值給shared_ptr或weak_ptr。 指針所指對象在所有的相關聯的shared_ptr生命周期結束時結束,是強引用。 weak_ptr weak_ptr 它不能決定所指對象的生命周期,引用所指對象時,需要lock()成shared_ptr才能使用。 參考:http://blog.csdn.net/xt_xiaotian/article/details/5714477 https://my.oschina.net/hevakelcj/blog/465978 2. 迭代器
template<class T>struct __list_node { void* prev; void* next; T data;};template<class T, class Ref, class Ptr>struct __list_iterator { typedef __list_iterator <T, Ref, Ptr> self; typedef Ptr Pointer; typedef Ref refernce; typedef __list_node<T>* link_type; link_type node; bool operator==(const self& x) const {return node == x.node;} bool operator != (const self& x) const {return node !=x.node;} refernce operator*() const {return (*node).data;} pointer operator->() const {return &(operator*());} self& operator++() {node = (link_type) (*node).next;return *this;} self& operator++(int) {self tmp = *this; ++*this;return tmp;}self& operator--() {node = (link_type)((*node).prev); return *this;}self operator--() {self tmp = *this;--*this; return tmp;}} (1)前置++運算符的重載方式: 成員函數的重載: 函數類型& operator++() 友元函數的重載:friend 函數類型& operator++(類類型& ) (2)后置++運算符的重載方式: 成員函數的重載:函數類型& operator++(int) 友元函數的重載:friend 函數類型& operator++(類類型&, int)1、輸入迭代器:只讀,一次傳遞 為輸入迭代器預定義實現只有istream_iterator和istreambuf_iterator,用于從一個輸入流istream中讀取。一個輸入迭代器僅能對它所選擇的每個元素進行一次解析,它們只能向前移動。一個專門的構造函數定義了超越末尾的值??偸?,輸入迭代器可以對讀操作的結果進行解析(對每個值僅解析一次),然后向前移動。 2、輸出迭代器:只寫,一次傳遞 這是對輸入迭代器的補充,不過是寫操作而不是讀操作。為輸出迭代器的預定義實現只有ostream_iterator和ostreambuf_iterator,用于向一個輸出流ostream寫數據,還有一個一般較少使用的raw_storage_iterator。他們只能對每個寫出的值進行一次解析,并且只能向前移動。對于輸出迭代器來說,沒有使用超越末尾的值來結束的概念??傊敵龅骺梢詫懖僮鞯闹颠M行解析(對每一個值僅解析一次),然后向前移動。 3、前向迭代器:多次讀/寫 前向迭代器包含了輸入和輸出迭代器兩者的功能,加上還可以多次解析一個迭代器指定的位置,因此可以對一個值進行多次讀/寫。顧名思義,前向迭代器只能向前移動。沒有為前向迭代器預定義迭代器。 4、雙向迭代器:operator– 雙向迭代器具有前向迭代器的全部功能。另外它還可以利用自減操作符operator–向后一次移動一個位置。由list容器中返回的迭代器都是雙向的。 5、隨機訪問迭代器:類似于一個指針 隨機訪問迭代器具有雙向迭代器的所有功能,再加上一個指針所有的功能(一個指針就是一個隨機訪問迭代器),除了沒有一種“空(null)”迭代器和空指針對應?;旧峡梢赃@樣說,一個隨機訪問迭代器就像一個指針那樣可以進行任何操作,包括使用操作符operator[]進行索引,加某個數值到一個指針就可以向前或者向后移動若干個位置,或者使用比較運算符在迭代器之間進行比較。 參考:http://www.tuicool.com/articles/ZJ36Rf http://blog.csdn.net/shawvichan/article/details/17639763 http://www.cnblogs.com/yc_sunniwell/archive/2010/06/25/1764934.html
仿函數 functor
template <class T>struct identity {const T&operator()(const T& x) const {return x;}};template<class Pair>struct select1st {const typename Pair::first_type&operator() (const Pair& x) const {return x.first}};template <class Pair>struct select2nd {const typename Pair::second_type& operator()(const Pair& x) const {return x.second; }};template <class Arg,class Result>struct unary_function {typedef Arg argument_type;typedef Result result_type;};template <class Arg1, classArg2, class result>struct binary_function { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type;};重載operator() 實現類似函數調用方式。參考python。
參考:http://blog.csdn.net/cracker_zhou/article/details/51627811 http://blog.csdn.net/tianshuai1111/article/details/7687983 http://blog.csdn.net/Robin__Chou/article/details/49329985
1.class template
template<tyoename T>class complex{public: complex (T r = 0, T i = 0): re (r), im(i) {} complex& operator+= (const complex&); T real() const {return re;} T imag() const {return im;}private: T re,im; };{ complex<double> c1(1.5, 2.5); compex<int> c2(2, 6);}function templatetemplate<class T>inline const T&min (const T& a, const T& b) { return b<a?b:a;}class stone{public: stone(int w, int h, int we): _w(w), _h(h), _we(we) {} bool operator < (const stone& rhs) const {return _we < rhs._we;}private: int _w, _h, _we;};stone r1(2,3), r2(2,3),r3;r3 = min(r1, r2);此處編譯器會對函數模板進行推導 推導的結果是T類型為stone 調用stone:: operator <. 3. member template 成員模板
template <class T1, class T2>struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair():fist(T1()), second(T2()) {} pair(const T1& a, const T2& b): fist(a), second(b) {} template<class U1, clas U2> pair(const pair<U1, U2>& p):first(p.first), second(p.second) {}}//可以用于繼承類中class Base1{};class Derived1:public Base1 {};class Base2{};clss Dedived2:publlic Base2 {};pair<Derived1,Deived2> p;pair<Base1, Base2> p2(p);pair<Base1, Base2> p2(pair<Derived1, Derived2>());//Derived1是Base1, Derived2是Base2,所以可以。//example 2template <typename _Tp>class shred_ptr:public __shared_ptr<_Tp>{... template<typename _Tp1> explicit shared_ptr(_Tp1* __p):__shared_ptr<_Tp>(__p){}};Base1* ptr = new Derived1;//up-castshared_ptr<Base1> sptr(new Derived);//模擬up-cast如果要在外面定義成員模板,必須包含兩個模板形參表,類模板形參和自己的模板形參。首先是類模板形參表,然后是自己的模板形參表。 類似: template<class T, class Alloc> template <class I> void MyVector<T, Alloc>::assign(I first, I last) { cout<<"assign"<<endl; }
參考:http://blog.csdn.net/wuzhekai1985/article/details/6654034
函數沒有偏特化只有全特化。 參考: http://www.jb51.net/article/56004.htm http://blog.csdn.net/thefutureisour/article/details/7964682/ http://blog.csdn.net/gatieme/article/details/50953564
模板模板參數就是將一個模板作為另一個模板的參數。 所以最后一個例子不是。 另外還有無類型模板 : template
容器,迭代器, 算法, 仿函數,待補充。
此處用于tempalte paramters 就是template parameters pack (模板參數包) 個數可以用sizeof…(args) 來獲得。最后sizeof…(args) = 0 時調用第一個重載的空print函數什么都不做返回。
參考:http://www.cnblogs.com/tekkaman/p/3501122.html http://blog.csdn.net/wag2765/article/details/50581469 2. auto C++引入auto關鍵字主要有兩種用途:一是在變量聲明時根據初始化表達式自動推斷該變量的類型,二是在聲明函數時作為函數返回值的占位符。
list<string> c;list<string>::iterator ite;ite= find(c.begine(),c.end9),target);//可以用auto 代替auto = find(...)//但是不能寫為auto ite;ite = find(...)//此時編譯器無法推斷auto類型。//作為函數返回值占位符時,auto主要與decltype關鍵字配合使用,作為返回值類型后置時的占位符。此時,關鍵字不表示自動類型檢測,僅僅是表示后置返回值的語法的一部分。template<class T, class U>auto add(T t, U u) -> decltype(t + u) { return t + u;}參考:http://blog.csdn.net/Xiejingfa/article/details/50469045 http://www.2cto.com/kf/201404/293503.html 3.Range-base for
for (decl:coll){}for (int i : {1,2,3}){ cout << i << endl;}vector<double> vec;for (auto elem :vec) { cout << elem << endl;//按值傳遞}for (auto& elem: vec) { elem* = 3;//按引用傳遞可以改變包內內容}1.引用不能重新賦值 2. 引用不能單獨聲明 3. object 和其引用大小地址均相同 4. 引用不構成重載。 const 函數構成重載即func() const {} 構成重載由于其是函數簽名的一部分。 5. sizeof(值) = sizeof(引用)
構造由內而外 析構相反由外而內 構造先base 再component 最后自己。 Derived::Derived():Base():COmponent(){} 析構先自己再component最后base Derived::~Derived(){…~Component,~Base()}
新聞熱點
疑難解答
圖片精選