달력

2

« 2025/2 »

  • 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
2012. 9. 25. 14:51

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

#include
#include
#include
#include

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(*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 > VPW;

int main(){


    tr1::shared_ptr sw(new SpecialWindow());
    tr1::shared_ptr 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(new SpecialWindow()));
    vpw.push_back(tr1::shared_ptr(new Window()));

    for(VPW::iterator iter = vpw.begin(); iter != vpw.end(); ++iter)
    {
            //실제 객체에서 get()함수 호출
            iter->get()->get();
    }

}


'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