달력

0

« 2025/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

'c++'에 해당되는 글 11

  1. 2012.09.25 다형성과 tr1::shared_ptr을 이용한 벡터 사용
2012. 9. 25. 14:51

다형성과 tr1::shared_ptr을 이용한 벡터 사용 c++2012. 9. 25. 14:51

  1. #include<iostream>  
  2. #include<iostream>  
  3. #include<vector>  
  4. #include<tr1 memory="">  
  5.   
  6. using namespace std;  
  7.   
  8. class Window{  
  9.   
  10. public:  
  11.   
  12.     virtual void onResize(){num = 1;}  
  13.     int getNum() const { return num;}  
  14.     virtual void get() const{ cout << "Window" << endl;}  
  15. protected:  
  16.   
  17.     int num;  
  18. };  
  19.   
  20. class SpecialWindow: public Window{  
  21.   
  22. public:  
  23.     //virtual void onResize(){ static_cast<window>(*this).onResize();}  
  24.     //virtual void onResize(){ Window::onResize();}  
  25.     virtual void onResize(){ num = 2;}  
  26.   
  27.     virtual void get() const { cout << "SpecialWindow" << endl;}  
  28.   
  29. private:  
  30. };  
  31.   
  32. class ResearchWindow: public Window{  
  33.   
  34. public:  
  35.     virtual void get() const { cout << "ResearchWindow" << endl;}  
  36. };  
  37.   
  38. typedef vector<tr1::shared_ptr<window> > VPW;  
  39.   
  40. int main(){  
  41.   
  42.   
  43.     tr1::shared_ptr<specialwindow> sw(new SpecialWindow());  
  44.     tr1::shared_ptr<researchwindow> rw(new ResearchWindow());  
  45.   
  46.     //객체 얻기  
  47.     (*sw).onResize();  
  48.     cout << (*sw).getNum() << endl;  
  49.   
  50.     VPW vpw;  
  51.     vpw.push_back(sw);  
  52.     vpw.push_back(rw);  
  53.     vpw.push_back(tr1::shared_ptr<specialwindow>(new SpecialWindow()));  
  54.     vpw.push_back(tr1::shared_ptr<window>(new Window()));  
  55.   
  56.     for(VPW::iterator iter = vpw.begin(); iter != vpw.end(); ++iter)  
  57.     {  
  58.             //실제 객체에서 get()함수 호출  
  59.             iter->get()->get();  
  60.     }  
  61.   
  62. }  
  63. </window></specialwindow></researchwindow></specialwindow></tr1::shared_ptr<window></window></tr1></vector></iostream></iostream>  


'c++' 카테고리의 다른 글

c++ quiz3  (0) 2012.09.27
c++ strategy 패턴  (0) 2012.09.27
Quiz2  (0) 2012.09.26
c++ struct와 class의 차이점...?????  (0) 2012.09.25
가상함수  (0) 2012.09.25
:
Posted by НooпeУ


Code Start Code End