RenderTree渲染树
RenderTree渲染樹對(duì)類中的靜態(tài)成員有很重要的關(guān)系,這個(gè)和多態(tài)是有很重要的關(guān)系,舉個(gè)簡(jiǎn)單的例子,在游戲中,馬里奧需要渲染,蘑菇也需要渲染,怪獸也需要渲染,其是串在一個(gè)樹上的,但是不同的類型怎么將其掛在一起,在渲染的過程中,馬里奧,蘑菇和怪獸都是擁有不同的渲染接口,但是這樣渲染器會(huì)做的很復(fù)雜,并不是我們想要的,簡(jiǎn)單的方式就是創(chuàng)建一個(gè)抽象基類,讓馬里奧,蘑菇和怪獸都繼承同一個(gè)抽象基類,但是我們面臨的問題是,對(duì)于馬里奧,蘑菇和怪獸來(lái)說(shuō),他們的類型都是不一樣的,怎么才能夠穿在一個(gè)鏈表上
如上所示,提供一個(gè)純虛函數(shù)show,使得下面的函數(shù)可以繼承,每個(gè)子類中的show都是不一樣的
為了將這些東西連起來(lái),需要有個(gè)靜態(tài)的頭,這個(gè)是沒有問題的
要串成一個(gè)鏈表,我們要求所有的節(jié)點(diǎn)都是類型是一樣的,所以需要在每個(gè)節(jié)點(diǎn)加上next,因此這個(gè)鏈表本質(zhì)上是父類指針next連在一起的
1 #include <iostream> 2 using namespace std; 3 4 #include <thread> 5 #include <unistd.h> 6 //父類需要提供一個(gè)接口 7 class RenderShape 8 { 9 public: 10 virtual void show()=0; 11 bool init(int x,int y) 12 { 13 _x=x; 14 _y=y; 15 return true; 16 } 17 18 static void RenderShapeList() 19 { 20 RenderShape *t=head; 21 while(t) 22 { 23 t->show();//所有的地方可以直接實(shí)現(xiàn)覆寫 24 t=t->next; 25 } 26 } 27 28 protected: 29 int _x; 30 int _y; 31 static RenderShape *head;//這個(gè)是需要共享的 32 RenderShape *next; 33 }; 34 35 RenderShape *RenderShape::head=nullptr; 36 37 class Rect:public RenderShape 38 { 39 public: 40 Rect *create(int x,int y,int w,int l) 41 { 42 Rect *pRet=new Rect; 43 if(pRet&&pRet->init(x,y,w,l)) 44 { 45 pRet->autoRelease(); 46 } 47 else 48 { 49 delete pRet; 50 pRet=nullptr; 51 } 52 } 53 54 bool init(int x, int y,int w,int l) 55 { 56 RenderShape::init(x,y); 57 _w=w; 58 _l=l; 59 return true; 60 } 61 62 void autoRelease() 63 { 64 this->next=head; 65 head=this; 66 } 67 68 virtual void show() 69 { 70 cout<<"draw rect from"<<"("<<_x<<","<<")"<<_y 71 <<"width"<<_w<<"length"<<_l<<endl; 72 } 73 protected: 74 int _w; 75 int _l; 76 }; 77 78 class Circle:public RenderShape 79 { 80 public: 81 Circle *create(int x,int y,int r) 82 { 83 Circle *pRet=new Circle; 84 if(pRet&&pRet->init(x,y,r)) 85 { 86 pRet->autoRelease(); 87 } 88 else 89 { 90 delete pRet; 91 pRet=nullptr; 92 } 93 } 94 95 bool init(int x, int y, int r) 96 { 97 RenderShape::init(x,y); 98 _r=r; 99 return true; 100 } 101 102 void autoRelease() 103 { 104 this->next=head; 105 head=this; 106 } 107 108 virtual void show() 109 { 110 cout<<"draw circle from"<<"("<<_x<<","<<")"<<_y 111 <<"radius"<<_r<<endl; 112 } 113 114 protected: 115 int _r; 116 }; 117 118 class Ellipse:public RenderShape 119 { 120 protected: 121 int _l; 122 int _s; 123 }; 124 125 void threadTask() 126 { 127 while(1) 128 { 129 cout<<"++++++++++++++++++++++++++"<<endl; 130 RenderShape::RenderShapeList(); 131 sleep(2); 132 cout<<"--------------------------"<<endl; 133 } 134 } 135 int main() 136 { 137 Rect *pr; 138 Circle *pc; 139 140 thread t(threadTask); 141 while(1) 142 { 143 int choice; 144 cin>>choice; 145 switch(choice) 146 { 147 case 1: 148 pr=Rect::create(1,2,3,4); 149 break; 150 case 2: 151 pc=Circle::create(4,5,6); 152 break; 153 } 154 } 155 t.join(); 156 return 0; 157 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/Cucucudeblog/p/10224059.html
總結(jié)
以上是生活随笔為你收集整理的RenderTree渲染树的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 江南百景图城中故事怎么做?
 - 下一篇: 求一个qq个性网个性签名!