《C++設(shè)計(jì)新思維》讀書(shū)筆記(二)
《C++設(shè)計(jì)新思維》讀書(shū)筆記(二)
1.5 Policies和Policy Classes
舉例,定義一個(gè)policy生成對(duì)象:Creator policy 提供一個(gè)Create函數(shù),返回一個(gè)指向新生T類型對(duì)象的指針。
我們有三種做法:
這些實(shí)作出來(lái)的policy稱為policy classes,這個(gè)東西并不意圖被單獨(dú)使用,它們主要用于繼承或被內(nèi)含于其它c(diǎn)lasses。
一個(gè)類以復(fù)合或繼承的方式使用先前定義的三個(gè)classes之一,例如
如果class采用一個(gè)或多個(gè)policies,我們稱為hosts或host classes。
客戶端如此實(shí)例化:
讓我們分析整個(gè)來(lái)龍去脈。無(wú)論何時(shí),當(dāng)一個(gè)MywidgetMgr對(duì)象需要產(chǎn)生一個(gè)widget對(duì)象時(shí),它便調(diào)用它的policy子對(duì)象OpNewCreator<widget>所提供的Createv()。選擇“生成策略”(Creation policy)是WidgetManager使用者的權(quán)利。藉由這樣的設(shè)計(jì),可以讓W(xué)idgetManager使用者自行裝配他所需要的機(jī)能。
這便是Policy-based class的設(shè)計(jì)主旨。
==============================
當(dāng)T為基類,具有派生類時(shí),兩者申請(qǐng)的內(nèi)存大小是不一致的,sizeof(T)不包括 virtual table 的大小。
——鐘遙
//////////////////////////////
鐘遙過(guò)慮了,以下代碼
輸出結(jié)果:
1.5 Policies和Policy Classes
舉例,定義一個(gè)policy生成對(duì)象:Creator policy 提供一個(gè)Create函數(shù),返回一個(gè)指向新生T類型對(duì)象的指針。
我們有三種做法:
1 template<class T>
2 struct OpNewCreator{
3 static T* Create(){
4 return New T;
5 }
6 };
7 template <class T>
8 struct MallocCreator{
9 static T* Create(){
10 void* buf = std::malloc(sizeof(T));
11 if(!buf) return 0;
12 return new(buf) T; //定位new表達(dá)式,見(jiàn)《C++ primer》8.4.5, cuigang
13 }
14 };
15 template <class T>
16 struct PrototypeCreator{
17 PrototypeCreator(T* pObj =0):pPrototype_(pObj){}
18 T* Create(){
19 return pPrototype_? pPrototype_->Clone():0;
20 }
21 T* GetPrototype(){ return pPrototype_;}
22 void SetPrototype(T* pObj){pPrototype_ = pObj;}
23 private:
24 T* pPrototype_;
25 };
26
2 struct OpNewCreator{
3 static T* Create(){
4 return New T;
5 }
6 };
7 template <class T>
8 struct MallocCreator{
9 static T* Create(){
10 void* buf = std::malloc(sizeof(T));
11 if(!buf) return 0;
12 return new(buf) T; //定位new表達(dá)式,見(jiàn)《C++ primer》8.4.5, cuigang
13 }
14 };
15 template <class T>
16 struct PrototypeCreator{
17 PrototypeCreator(T* pObj =0):pPrototype_(pObj){}
18 T* Create(){
19 return pPrototype_? pPrototype_->Clone():0;
20 }
21 T* GetPrototype(){ return pPrototype_;}
22 void SetPrototype(T* pObj){pPrototype_ = pObj;}
23 private:
24 T* pPrototype_;
25 };
26
這些實(shí)作出來(lái)的policy稱為policy classes,這個(gè)東西并不意圖被單獨(dú)使用,它們主要用于繼承或被內(nèi)含于其它c(diǎn)lasses。
一個(gè)類以復(fù)合或繼承的方式使用先前定義的三個(gè)classes之一,例如
1 //Library code
2 template <class CreationPolicy>
3 class WidgetManager : public CreationPolicy{};
2 template <class CreationPolicy>
3 class WidgetManager : public CreationPolicy{};
如果class采用一個(gè)或多個(gè)policies,我們稱為hosts或host classes。
客戶端如此實(shí)例化:
1 //Application code
2 typedef WidgetManager< OpNewCreator<widget> > MywidgetMgr;
2 typedef WidgetManager< OpNewCreator<widget> > MywidgetMgr;
讓我們分析整個(gè)來(lái)龍去脈。無(wú)論何時(shí),當(dāng)一個(gè)MywidgetMgr對(duì)象需要產(chǎn)生一個(gè)widget對(duì)象時(shí),它便調(diào)用它的policy子對(duì)象OpNewCreator<widget>所提供的Createv()。選擇“生成策略”(Creation policy)是WidgetManager使用者的權(quán)利。藉由這樣的設(shè)計(jì),可以讓W(xué)idgetManager使用者自行裝配他所需要的機(jī)能。
這便是Policy-based class的設(shè)計(jì)主旨。
==============================
1 template <class T>
2 void* buf = std::malloc(sizeof(T));
3 void* buf = (void*)new T;
2 void* buf = std::malloc(sizeof(T));
3 void* buf = (void*)new T;
當(dāng)T為基類,具有派生類時(shí),兩者申請(qǐng)的內(nèi)存大小是不一致的,sizeof(T)不包括 virtual table 的大小。
——鐘遙
//////////////////////////////
鐘遙過(guò)慮了,以下代碼
1 struct base{
2 int x;
3 base(){
4 std::cout<< "create a base."<<std::endl;
5 }
6 virtual void foo(){
7 std::cout<<"call base"<<std::endl;
8 };
9 };
10 struct test : public base{
11 int y;
12 test(){
13 std::cout<< "create a test."<<std::endl;
14 }
15 virtual void foo(){
16 std::cout<<"call test."<<std::endl;
17 }
18 void foo2(){};
19 };
20 /////////////////////////
21 int a = sizeof(test);
22 int b = sizeof(base);
23 std::cout<<"sizeof(test)="<< a << ", "<<"sizeof(base)=" << b << endl;
24 base* pa = new base;
25 base* pb = new test;
26 std::cout<<"---------"<<endl;
27 base* ppa = (base*)malloc(sizeof(base));
28 base* ppb = (base*)malloc(sizeof(test));
29 std::cout<<"---------"<<endl;
30 new(ppa) base;
31 new(ppb) test;
32 pa->foo();
33 pb->foo();
34 ppa->foo();
35 ppb->foo();
2 int x;
3 base(){
4 std::cout<< "create a base."<<std::endl;
5 }
6 virtual void foo(){
7 std::cout<<"call base"<<std::endl;
8 };
9 };
10 struct test : public base{
11 int y;
12 test(){
13 std::cout<< "create a test."<<std::endl;
14 }
15 virtual void foo(){
16 std::cout<<"call test."<<std::endl;
17 }
18 void foo2(){};
19 };
20 /////////////////////////
21 int a = sizeof(test);
22 int b = sizeof(base);
23 std::cout<<"sizeof(test)="<< a << ", "<<"sizeof(base)=" << b << endl;
24 base* pa = new base;
25 base* pb = new test;
26 std::cout<<"---------"<<endl;
27 base* ppa = (base*)malloc(sizeof(base));
28 base* ppb = (base*)malloc(sizeof(test));
29 std::cout<<"---------"<<endl;
30 new(ppa) base;
31 new(ppb) test;
32 pa->foo();
33 pb->foo();
34 ppa->foo();
35 ppb->foo();
輸出結(jié)果:
1 sizeof(test)=12, sizeof(base)=8
2 create a base.
3 create a base.
4 create a test.
5 ---------
6 ---------
7 create a base.
8 create a base.
9 create a test.
10 call base
11 call test.
12 call base
13 call test.
2 create a base.
3 create a base.
4 create a test.
5 ---------
6 ---------
7 create a base.
8 create a base.
9 create a test.
10 call base
11 call test.
12 call base
13 call test.
posted on 2007-12-17 21:41 cuigang 閱讀(643) 評(píng)論(0) 編輯 收藏 引用 所屬分類: 《C++設(shè)計(jì)新思維》讀書(shū)筆記