c++질문

조회수 396회

include

using namespace std; class Point { private: int x; int y; static int numCreatedObjects; public: Point() : x(0), y(0) { numCreatedObjects++; } // int _x 와 int _y를 입력으로 받는 생성자 Point(int _x, int _y) : x(_x), y(_y) { numCreatedObjects++; } ~Point() { cout << "Destructed..." << endl; } void setXY(int _x, int _y) { //this-> 사용한 초기화 this->x = _x; this->y = _y; } int getX() const { return x; } int getY() const { return y; } // *this + pt2 -> Point operator+(Point& pt2) { Point result(this->x + pt2.getX(), this->y + pt2.getY()); return result; } //operator overloading(연산자 오버로딩) Point& operator=(Point& pt) { this->x = pt.getX(); this->y = pt.getY(); return *this; } static int getNumCreatedObject() { return numCreatedObjects; } friend void print(const Point& pt); friend ostream& operator<<(ostream& cout, Point& pt); friend class SpyPoint; }; //static 맴버 변수 초기화 (numCreatedObjects) int Point::numCreatedObjects = 0; //객체 call by reference시: const로 함수 입력시 const method만 함수에서 사용가능 // const: 객체 내부의 member data가 상수(변하지 않는다) void print(const Point& pt) { cout << pt.x << ", " << pt.y << endl; } //Point operator+(Point& pt1, Point& pt2){ // Point result(pt1.getX() + pt2.get(X), pt1.getY() + pt2.getY()); // return result; //} ostream& operator<<(ostream& cout, Point& pt) { return cout << pt.x << ", " << pt.y; } class SpyPoint { public: //다음과 같이 출력 되도록 hack_all_info함수 구현 //Hacked by SpyPoint //x: 40 //y: 60 //numCreatedObj.: 10 void hack_all_info(Point& pt) { cout << "Hacked by SpyPoint" << endl << "x: " << pt.x << endl << "y: " << pt.y << endl << "numCreatedObj.: " << Point::getNumCreatedObject() << endl <<endl; } }; int main() { Point pt1(1, 2); cout << "pt1 : "; print(pt1); cout << endl;

// 포인터
Point* pPt1 = &pt1;
// pPt1의 값을 통해 getX, getY를 호출하여 출력
cout << "pt1 : ";
cout << (*pPt1).getX() << ", " << (*pPt1).getY() << endl;
// pPt1를 통해 호출 getX, getY를 호출하여 출력
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;

cout << endl;

//동적으로 Point* pPt2할당하여 10,20넣은 뒤 ->사용하여 출력(pt1 출력 참고)
Point* pPt2;
pPt2 = new Point(10, 20);
cout << "pt2 : ";
cout << pPt2->getX() << ", " << pPt2->getY() << endl;
cout << endl;

//pPt1, pPt2의 메모리 해제

delete pPt2;

cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl;

// 연산자 오버로딩
//pt4 = pt2, pt3값 더하기
Point pt2(10, 20);
Point pt3(30, 40);
Point pt4 = pt2 + pt3;
cout << "pt2 : ";
cout << pt2 << endl;
cout << "pt3 : ";
cout << pt3 << endl;
cout << "pt4 : ";
cout << pt4 << endl;

cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl << endl;
// object array
Point* ptAry = new Point[5];
cout << "pt2 NumCreatedObject : ";
cout << pt2.getNumCreatedObject() << endl;
cout << endl;

// ptAry 메모리 해제
delete[] ptAry;

cout << endl;

// friend class
SpyPoint spy;
cout << "pt1 info" << endl;
spy.hack_all_info(pt1);
cout << "pt4 info" << endl;
spy.hack_all_info(pt4);

return 0;

} 이거 실행시키면 pt1 : 1, 2

pt1 : 1, 2 pt1 : 1, 2

pt2 : 10, 20

Destructed... pt1 NumCreatedObject : 2 Destructed... pt2 : 10, 20 pt3 : 30, 40 pt4 : 40, 60 pt1 NumCreatedObject : 5

pt2 NumCreatedObject : 10

Destructed... Destructed... Destructed... Destructed... Destructed...

pt1 info Hacked by SpyPoint x: 1 y: 2 numCreatedObj.: 10

pt4 info Hacked by SpyPoint x: 40 y: 60 numCreatedObj.: 10

Destructed... Destructed... Destructed... Destructed... 이렇게 나오는데

Destructed... pt1 NumCreatedObject : 2 Destructed...이거를

Destructed... Destructed... pt1 NumCreatedObject : 2 이렇게 바꿀 수는 없나요? pt1 NumCreatedObject : 2밑에 Destructed...가 왜 생기는지를 모르겠어요

1 답변

  • c++은 잘 모르지만 디스트럭터라는 건 "이걸 실행하지 않으면 이 인스턴스는 디스트럭트가 안된다" 하는 것을 규정해놓은 것 아닌지요? 그렇다면, 이따금 조건부로 "pt1 NumCreatedObject"를 출력할 수 있는 인스턴스에게 "Destructed..."를 출력하라는 디스트럭터가 주어져있다고 할 때, 그 인스턴스는 "pt1 NumCreatedObject"를 출력하건 안 하건 항상 맨 끝에 "Destructed..."를 출력하겠지요. 태어난 것은 항상 죽게 마련이고 생성된 인스턴스는 항상 그 끝이 소멸이니까요.

    제가 c++을 모르는 채로 드릴 수 있는 의견은 여기까지인 거 같네요. 나머지는 다른 괴수분이 봐주시길 바라 보아야겠네요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)