首頁>技術>

1、轉換函式,例如類有兩個整形資料成員{分子,分母},類的建構函式設計的時候,預設分母為1,這在數學形式中,整數就可以用分子除以1,還是等於分子。

將分子分母轉換為分數形式,即小數形式

operator double(){... // 具體轉換實現}

將分子分母轉換為字串形式,並將分子分母拼接起來

operator string(){... // 具體的實現}

2、non-explicit-one-argument constructor 非顯示宣告只有一個引數的建構函式,如果有兩個引數,但後一個引數有預設值,也是可以的

3、用類來模仿指標-如智慧指標,迭代器

4、用類來模仿函式 -

template <class T>struct identity{public:    const T& operator()(const T& x){ return x; }};

5、類模板

template <typename 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 img() const { return im; }private:	T re,im;	friend complex& __doapl(complex*, const complex&);};int main(){	complex<double> c1(2.5,1.5);	complex<int> c2(2,1);}

6、函式模板

template<typename 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),_weight(we){}	bool operator<(const stone& rhs) const {		return _weight < rhs._weight;	}private:	int _w,_h,_weight;};int main(){	stone r1(2,3),r2(3,3),r3;	r3 = min(r1,r2);}

7、成員模板

template <class T1, class T2>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 <class U1,class U2>	pair(const pair<U1,U2>& p):first(p.first),second(p.second){}};
class Base1{};class Derived1:public Base1{};class Base2{};class Derived2:public Base2{};pair<Derived1,Derived2> p;pair<Base1,Base2> p2(p);// ==pair<Base1,Base2> p2(pair<Derived1,Derived2>());

把一個由鯽魚和麻雀構成的pair放進由一個魚類和鳥類構成的pair中,是可以的,但是反過來是不可以的。

template<typename _Tp>class shared_ptr:public __shared_ptr<_Tp>{	template<typename _Tp1>	explicit shared_ptr(_Tp1* __p):shared_ptr<_Tp>(__p){}};Base1 *ptr = new Derived1; // up-castshare_ptr<Base1> sptr(new Derived1); // up-cast

8、模板特化(specialization)

-模板全特化

template <class Key>struct hash{};template<>struct hash<char>{	size_t operator()(char x)const{return x;}};template<>struct hash<int>{	size_t operator()(int x)const{return x;}};template<>struct hash<long>{	size_t operator()(long x)const{return x;}};int main(){	cout << hash<long>()(1000) << endl;}

-模板偏特化(個數偏特化,範圍偏特化)

(1)個數的偏

template <typename T,template Alloc = ...>class vector{	};template <typename Alloc = ...>class vector<bool,Alloc>  // 繫結bool型別{	};

(2)範圍上的偏

template <typename T>class C{	};template <typename T>class C<T*>{	};C<string> obj1;C<string*> obj2;

9、模板模板引數 - 指的是允許使用者用第一個模板引數作為預設引數時,允許使用任意容器型別被組合起來;但是如果指定的容器存在第二模板引數的話,就不可以了。

list存在第二模板引數。

例如shared_ptr、auto_ptr只有第一模板引數,unique_ptr、weak_ptr存在第二模板引數。以下為模板庫中的原始碼

但是我看了下智慧指標,weak_ptr好像不存在第二模板引數。

下面這個例子不是模板模板引數

所以模板主要分為三大類,類模板,函式模板,成員函式模板。

10、C++ 2.0{

1、數量不定的模板引數(variadic templates --- since c++11)

#include <iostream>#include <bitset>using namespace std;// 數量不定的模板引數// variadic templates(since C++11)void print() // 等零個引數的時候,會呼叫這個函式{	}template <typename T,typename... Types>void print(const T& firstArg,const Types&...args){	cout << firstArg << endl;	print(args...);}int main(){	print(7.5,"hello",bitset<16>(377),42);	return 0;}

Inside variadic templates,sizeof...(args) yeilds the number of arguments...就是一個所謂的pack(包)用於template parameter,就是template parameters pack(模板包)用於function paramters types,就是function parameter type pack(函式引數型別包)用於function parameters,就是function parameters pack(函式引數包)

2、auto ( since c++11)

list<string> c;list<string>::iterator ite;ite = find(c.begin(),c.end(),target);list<string> b;auto ite = find(b.begin(),b.end(),target);// 但是auto不能單獨定義或宣告,如下list<string> a;auto ite; // 必須要編譯器給你推斷返回值的型別,不能單獨定義ite = find(a.begin(),a.end(),target);

3、ranged-base for(since C++ 11)

for(decl:coll){	statement}for(int i:{2,3,5,7,9,13,17,19}){ // 大括號形成的容器	cout << i << endl;}vector<double> vec;...for(auto elem :vec){ // pass by value	cout << elem <<endl; // 如果*3的話,不會影響vec的值}for(auto& elem: vec){ // pass by reference	elem *= 3; // 會影響vec的值,會隨之改變}

} // C++2.0

11 、Reference(引用)

int main(){	int x = 0;	int* p = &x;	int& r = x;  // r代表x,現在r,x都是0	int x2 = 5;	r = x2;       // r不能重新代表其他物品,現在r,x都是5	int& r2 = r;  // r2是5(r2代表r,亦相當於代表x,現在都是5)}

object和其reference的大小相同,地址也相同,從邏輯上來看這些全部都為假象,但是為了方便理解,因此做了如此實現。

reference通常不用於宣告變數,常用於引數型別(parameters type)和返回值型別(return type)的描述。

12、物件模型(Object Modal):關於vptr和vtbl(虛指標和虛表)

class A{public:	virtual void vfunc1();	virtual void vfunc2();	void func1();	void func2();private:	int m_data1,m_data2;};class B : public A{public:	virtual void vfunc1();	void func2();private:	int m_data3;};class C : public B{public:	virtual void vfunc1();	void func2();private:	int m_data1,m_data4;};

例如繪製不同的行狀,頂層類設計為行狀,子類設計為各式各樣的行狀來繼承行狀基類,例如圓形、橢圓形、矩形、正方向、三角形,然後各自重寫父類的draw虛擬函式,分別呼叫自己的繪圖方法。

動態呼叫的條件,(1)必須是一個指標,(2)這個指標必須是向上轉型的(子類指標轉為父類指標),(3)呼叫的是虛擬函式。

即呼叫(* (p->vptr[n]))(p)或者(* p->vptr[n])(p),稱為虛機制,即動態繫結。

虛擬函式的這種用法,稱之為多型。同一個指標指向不同的物件。

13、物件模型:關於this

14、動態繫結(Dynamic Binding)

15、談談const

成員函式後面加const,表示這個成員函式不改變類的資料成員。

4
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 併發程式設計案例:高效能限流器Guava RateLimiter