1.導讀
2.Conversion function
3.non-explicit-one-argument ctor
4.point-link classes
5.function-like classes
6.namespace 經驗談
7.class template 類模板
8.function template 函數模板
9.member template 成員模板
10.specialization 模板特化
11.模板偏特化
12.模板模板參數
13.關于C++標準庫
14.三個主題
15.Reference
1.Conversion Function轉換函數
class Fraction{
public:
operator double() const {//轉換不可能改變類里的成員,通常加上const
return (double)(m_numerator / m_denominator);
}//把Fraction對象轉換成double類型,double()不可有參數,返回類型不用寫
private:
int?m_numerator;
int?m_denominator;
};
Fraction f(3,5);
double d=4+f;//調用operator double()將f轉為0.6
2.non-explict it one arguement construct
//可以把別的東西轉換為這種東西
class Fraction{
public:
Fraction(int num,int den=1):m_numerator(num),m_denominator(den) { ?}
operator double const {
return (double)(m_numerator / m_denominator);
}
Fraction operator+(constFraction& f) {
return
Fraction(...);
private:
int?m_numerator;
int?m_denominator;
};
Fraction f(3,5);
Fractiond2=f+4;//調用non_explict ctor將4轉為Fraction,然后調用operator+
template
calss?vector//模板的先特化,容器里的每個元素都是ool值
{
public:
typedef?__bit_reference?reference;
protected:
reference?operator[](size?type?n){//代表本來真正傳回來的東西
return?*(begin()?+diffenece_type(n));
}
struct?__bit_reference
{
unsigned?int*?p;
unsigned?int?mask;
...
public:
operator?bool()?const?{?return?!(!(*p?*?mask));}
};
3.pointer-like classes,
template
class?shared_ptr{
public:
T&?operator*()?const
{
return?*px;
}
T*?operator->()?const{
return?px;
}
shared_ptr(T*?p):px(p)?{?}
private:
T*?px;//指向T的指針,什么類型都接受
long*?pn;
};
lass?Foo{
...
void?method(void)?{...}
};
shared_ptr?sp(new?Foo);
Foo?f(*sp);
sp->method();//調用method()
px->method();//通過這個對象調用這個函數
箭頭符號有一個特殊的行為:作用下去得到的結果,這個箭頭符號會繼續作用下去
智能指針里頭一定帶著一個一般的指針,而智能指針一定要帶*和->符號
point-class這種類型
4迭代器
主要用來遍歷容器
template
struct?_list_iterator//鏈表的迭代器
{
typedef?_list_iterator?self;
typedef?Ptr?pointrt;
typedef?Ref?reference;
typedef?_list_node*?linx_type;//指向結點的指針
link_type?node;
bool?operator?==?(const?self&?x)?const?{return?node?==?x.node;}
bool?operator?!=?(const?self&?x)?const?{return?node?!=?x.node;}
reference?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--(int)?{self?tmp?=?*this;?--*this;?return?tmp;}
};
5?Function - like class
template
struct?identity : public unart_function{
const?T&?operator()(const?T&?x)?const?{?return?x;}
};
template
struct?selectst{//取出第一個
const?typename?Pair::first_type&?operator()?(const?Pair&?x)?const
{
return?x.first;
}
};
template
Struct?select2nd : publicunart_function{//取出第二個
const?typename?Pair::second_type&?operator()(const?Pair&?x)?const
{
return?x.second;
}
};
template
struct?pair: publicunart_function{
T1?first;
T2?second;
pair()?:?first(T1()),second(T2())?{}
pair(const?T1&?a,const?T2&?b):first(a),second(b)?{?}
};
6.class template類模板和fumction template函數模板
template//復數的類模板
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;
friend?complex&?__doapl(complex*,const?complex&);
};
{
complex?c1(2.5,1.5);
complex?c2(2,6);
}
函數模板:
class?stone{
public:
stone(int?w,int?h,int?we):_w(w),_h(h),_weight(we)?{}
bool?operator<<(const?stone&?rhs)?const?{
return?_weight?<?rhs._weight;
}
private:
int?_w,_h,_weight;
};
template
inline const T& min(const T& a, const T& b)
{ ?return b;}
函數模板在使用的時候不必指明type,能夠通過調用時傳遞的實參推出來類型
member template成員模板
template
struct?pair{
typedef?T1?first_type;
typedef?T2?second_type;
T1?first;
T2?second;
pair():first(T1()),second(T2())?{}
pair(const?T1?&a,const?T2?&b)
:first(a),second(b)?{}
template? ?//成員模板
pair(const?pait?&p)//T1和T2類型確定了以后,U1和U2也能進行確定
:first(p.first),second(p.second)?{}
}
7.模板特化specialization
Template
struct?hash ();
template<>
struct?hash{
size_t?operator()?(char?x)?const?{return?x;}//重載
};
template<>
struct?hash{
size_t?operator()?(int?x)?const?{return?x;}
};
template<>
struct?hash{
size_t?operator()?(long?x)?const?{return?x;}
};
作者:孫浩_9bfd
鏈接:http://www.lxweimin.com/p/5f845320f773
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。