개체를 할당하는 데 사용 된 메모리를 해제 할 수 있습니까? 그렇다면 어떻게 할 수 있습니까? 내가 동적 메모리 할당을 사용하는 경우메모리 해제 (가능한 경우)
class CRectangle {
int width, height;
public:
CRectangle (int,int);
~CRectangle();
int area() {
return (width * height);
}
};
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
CRectangle::~CRectangle() {
// Do something here
}
이 될 것입니다 :
class CRectangle {
int *width, *height;
public:
CRectangle (int,int);
~CRectangle();
int area() {
return (*width * *height);
}
};
CRectangle::CRectangle (int a, int b) {
width = new int;
height = new int;
*width = a;
*height = b;
}
CRectangle::~CRectangle() {
delete width
delete height
}
그들은 동일한 출력을 할, 그래서 동적 메모리 할당을 사용하는 장점은 무엇인가?
에서 객체 자체의 크기 이외의 메모리 할당은 없다 그 코드에서 :
는 다음과 같이 그 나타낸 것입니다. –