2012. 9. 25. 14:51
다형성과 tr1::shared_ptr을 이용한 벡터 사용 c++2012. 9. 25. 14:51
- #include<iostream>
- #include<iostream>
- #include<vector>
- #include<tr1 memory="">
- using namespace std;
- class Window{
- public:
- virtual void onResize(){num = 1;}
- int getNum() const { return num;}
- virtual void get() const{ cout << "Window" << endl;}
- protected:
- int num;
- };
- class SpecialWindow: public Window{
- public:
- //virtual void onResize(){ static_cast<window>(*this).onResize();}
- //virtual void onResize(){ Window::onResize();}
- virtual void onResize(){ num = 2;}
- virtual void get() const { cout << "SpecialWindow" << endl;}
- private:
- };
- class ResearchWindow: public Window{
- public:
- virtual void get() const { cout << "ResearchWindow" << endl;}
- };
- typedef vector<tr1::shared_ptr<window> > VPW;
- int main(){
- tr1::shared_ptr<specialwindow> sw(new SpecialWindow());
- tr1::shared_ptr<researchwindow> rw(new ResearchWindow());
- //객체 얻기
- (*sw).onResize();
- cout << (*sw).getNum() << endl;
- VPW vpw;
- vpw.push_back(sw);
- vpw.push_back(rw);
- vpw.push_back(tr1::shared_ptr<specialwindow>(new SpecialWindow()));
- vpw.push_back(tr1::shared_ptr<window>(new Window()));
- for(VPW::iterator iter = vpw.begin(); iter != vpw.end(); ++iter)
- {
- //실제 객체에서 get()함수 호출
- iter->get()->get();
- }
- }
- </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 |