생성자 소멸자 프로그램예제의 결과가 나오게 된 순서를 가르쳐주세요
조회수 512회
# include <iostream>
using namespace std ;
class Point {
int x, y ;
public:
Point(int x=0, int y=0) {
this->x = x ; this->y = y ;
cout << "\tPoint " ; print() ; cout << " constructed." << endl ;
}
~Point() { cout << "\tPoint " ; print() ; cout << " destructed." << endl ; }
void print() const { cout << "(" << x << ", " << y << ")" ; }
} ;
class Rectangle {
Point rightBottom, leftTop ;
public:
Rectangle(const Point& p1, const Point& p2=Point(0,0)) : leftTop(p1), rightBottom(p2) {
cout << "Rectangle: " ; print() ; cout << " constructed." << endl ;
}
Rectangle(int x1, int y1, int x2=0, int y2=0) : leftTop(x1, y1), rightBottom(x2, y2) {
cout << "Rectangle: " ; print() ; cout << " constructed." << endl ;
}
Rectangle() { cout << "Rectangle: " ; print() ; cout << " constructed." << endl ; }
~Rectangle() { cout << "Rectangle: " ; print() ; cout << " destructed." << endl ; }
void print() const { leftTop.print() ; rightBottom.print() ; }
} ;
int main(){
Point p(2, 2);
Rectangle r2(p);
}
의 결과가
가 되는데 프로그램이 어떤순서를 거쳐서 이와 같은 결과가 나오는지 모르겠습니다. 도와주세요 ㅠㅠ
1 답변
-
로그를 조금 더 추가하고 실행해 봤습니다.
컴파일러는 VS2015 를 사용했습니다.
# include <iostream> using namespace std; class Point { int x, y; public: Point(int x = 0, int y = 0) { this->x = x; this->y = y; cout << "\tPoint "; print(); cout << " constructed." << endl; } ~Point() { cout << "\tPoint "; print(); cout << " destructed." << endl; } void print() const { cout << "(" << x << ", " << y << ") " << this ; } }; class Rectangle { Point rightBottom, leftTop; public: Rectangle(const Point& p1, const Point& p2 = Point(0, 0)) : leftTop(p1), rightBottom(p2) { cout << "Rectangle: "; print(); cout << " constructed." << endl; } Rectangle(int x1, int y1, int x2 = 0, int y2 = 0) : leftTop(x1, y1), rightBottom(x2, y2) { cout << "Rectangle: "; print(); cout << " constructed." << endl; } Rectangle() { cout << "Rectangle: "; print(); cout << " constructed." << endl; } ~Rectangle() { cout << "Rectangle: "; print(); cout << " destructed." << endl; } void print() const { leftTop.print(); rightBottom.print(); } }; int main() { Point p(2, 2); cout << "1-------1-------1-------" << endl; { Rectangle r2(p); cout << "2-------2-------2-------" << endl; } cout << "3-------3-------3-------" << endl; }
결과는 이렇게 나옵니다.
1| Point (2, 2) 00AFFBA0 constructed. 2| 1-------1-------1------- 3| Point (0, 0) 00AFFAB8 constructed. 4| Rectangle: (2, 2) 00AFFB90(0, 0) 00AFFB88 constructed. 5| Point (0, 0) 00AFFAB8 destructed. 6| 2-------2-------2------- 7| Rectangle: (2, 2) 00AFFB90(0, 0) 00AFFB88 destructed. 8| Point (2, 2) 00AFFB90 destructed. 9| Point (0, 0) 00AFFB88 destructed. A| 3-------3-------3------- B| Point (2, 2) 00AFFBA0 destructed.
- 가장 간단한 걸 먼저 보면, 1번라인에서 생성되고, B번라인에서 소멸된
Point(2, 2)
입니다. 이것은, 코드의main
함수의 첫줄에 선언되어 생성된Point p(2, 2);
인스턴스입니다.1----1----1----
로그 전에 생성되었고,main
을 나가면서 (3----3-----3----
로그 이후에) 소멸되었습니다. - Rectangle 은 4번라인에서 생성자의 로그가 남았고, 7번라인에서 소멸자의 로그가 남았습니다.
- 3번라인에서 생성되고, 5번라인에서 소멸된
Point(0,0)
은 무엇일까요? 이것은Rectangle
첫번째 생성자 (Rectangle(const Point& p1, const Point& p2 = Point(0, 0))
)에서 두번째 인자가 없기 때문에 임시로 만들어지는Point(0, 0)
입니다. - 이제 의문으로 남는 라인은 8, 9번 라인의 소멸자 로그입니다. 이것은 인스턴스 포인터값을 봤을 때, 생성자 로그가 없이 소멸자로그만 남았습니다. 과연 무엇일까요? 답은, 이들은 메인의 내부 스코프에 선언된
r2
의 두개의 멤버포인트입니다. 이들 소멸자로그는2----2----2----
로그 이후에, 내부 스코프를 나가면서,r2
가 소멸되면서, 멤버변수 인스턴스들도 소멸이 된 것입니다. 그런데, 왜 생성자 로그는 안 찍혔느냐 하면, 이들이 생성된 것은, 코드상에 명시적으로 정의된Point
클래스의 두 개의 생성자로 만들어지지 않았기 때문입니다. 아마 암묵적으로 만들어진Point(const Point&)
생성자를 통해 생성되었을 것입니다. - (추가)
Point(2,2) 00AFFB90
,Point(0,0) 00AFFB88
은r2
의 생성자 정의Rectangle(const Point& p1, const Point& p2 = Point(0, 0)) : leftTop(p1), rightBottom(p2) { ... }
중에서leftTop(p1), rightBottom(p2)
부분에서 생성되었을 겁니다. 즉,r2.leftTop
,r2.rightBottom
에 해당하는 것입니다. 이 두Point
는main
함수의 지역변수p
와 임시로 만들어진Point(0, 0)
을 따라서 만들어진 것이지만,p
와 임시Point(0, 0)
와는 다른 인스턴스가 되겠죠. 이 둘이 소멸자만 로그에 남은Point
인스턴스입니다.
- 가장 간단한 걸 먼저 보면, 1번라인에서 생성되고, B번라인에서 소멸된
댓글 입력