C++/열혈 C++ 프로그래밍(윤성우 저)

열혈 C++ 프로그래밍 13-2 문제

꾸준함. 2017. 6. 15. 13:15

/*

만약에 Chapter 11을 공부하면서 스마트 포인터도 공부를 했다면, 이문제를 반드시 해결하고 넘어가기 바란다.

! 그럼 문제를 제시하겠다. 우리는 앞서 Chapter 11에서 다음의 형태로 스마트 포인터를 정의하였다.

class SmartPtr

{

private:

        Point *posptr;

public:

        SmartPtr(Point *ptr) :posptr(ptr)

        {

        }

        Point &operator*() const

        {

               return *posptr

        }

        Point *operator->() const

        {

               return posptr;

        }

        ~SmartPtr()

        {

               delete posptr;

        }

};

이 스마트 포인터를 템플릿으로 정의하여, 어떠한 클래스의 객체도 참조할 수 있는 포인터가 되게하자.

그리고는 아래의 Point 클래스와 main 함수를 기반으로 예제를 완성해보자.

참고로 스마트 포인터는 이렇듯 템플릿의 형태로 정의가 된다

class Point

{

private:

        int xpos, ypos;

public:

        Point(int x = 0, int y = 0) :xpos(x), ypos(y)

        {

        }

        void SetPos(int x, int y)

        {

               xpos = x;

               ypos = y;

        }

        void ShowPosition() const

        {

               cout << '[' << xpos << ", " << ypos << ']' << endl;

        }

};

 

int main(void)

{

        SmartPtr<Point> sptr1(new Point(1, 2));

        SmartPtr<Point> sptr2(new Point(3, 4));

        sptr1->ShowPosition();

        sptr2->ShowPosition();

        sptr1->SetPos(10, 20);

        sptr2->SetPos(30, 40);

        sptr1->ShowPosition();

        sptr2->ShowPosition();

        return 0;

}

*/

#include <iostream>

using namespace std;

 

class Point

{

private:

        int xpos, ypos;

public:

        Point(int x = 0, int y = 0) :xpos(x), ypos(y)

        {

        }

        void SetPos(int x, int y)

        {

               xpos = x;

               ypos = y;

        }

        void ShowPosition() const

        {

               cout << '[' << xpos << ", " << ypos << ']' << endl;

        }

};

 

template <typename T>

class SmartPtr

{

private:

        T *posptr;

public:

        SmartPtr(T *ptr) :posptr(ptr)

        {

        }

        T &operator*() const

        {

               return *posptr

        }

        T *operator->() const

        {

               return posptr;

        }

        ~SmartPtr()

        {

               delete posptr;

        }

};

 

int main(void)

{

        SmartPtr<Point> sptr1(new Point(1, 2));

        SmartPtr<Point> sptr2(new Point(3, 4));

        sptr1->ShowPosition();

        sptr2->ShowPosition();

        sptr1->SetPos(10, 20);

        sptr2->SetPos(30, 40);

        sptr1->ShowPosition();

        sptr2->ShowPosition();

        return 0;

}


개발 환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 열혈 C++ 프로그래밍 윤성우 저

반응형