C++ 얕은 복사 vs 깊은 복사(feat. 복사생성자)

Joosi_Cool·2022년 10월 10일
4

C++

목록 보기
17/20
post-thumbnail

얕은 복사, 깊은 복사란?

클래스에서 복사 생성자의 경우, 어떻게 복사하냐에 따라 얕은 복사가 될수도, 깊은 복사가 될수도 있다.

얕은 복사란(Shallow Copy)?

얕은 복사는 객체가 가진 멤버들의 값을 새로운 객체로 복사하는데 만약 개체가 참조타입의 멤버를 가지고 있다면 참조값만 복사가 된다.
얕은 복사의 경우 동적 할당을 받은 변수의 주소값을 공유한다.

예시코드

#include<iostream>
using namespace std;

class MyArray {
public:
	int size;
	int* data;

	MyArray(int size)
	{
		this->size = size;
		data = new int[size];
	}

	MyArray(const MyArray& other);

};

MyArray::MyArray(const MyArray& other) {
	this->size = other.size;
	this->data = other.data;
}



int main() {

	MyArray buffer(10);
	buffer.data[0] = 2;

	MyArray clone = buffer;
	buffer.data[0] = 3;

	cout << "clone.data[0] = " << clone.data[0] << endl;

	return 0;
}


결과처럼 참조로 받을 경우, 주소값을 공유한다. 따라서 복사한 대상자가 바뀌면 복사한 객체도 같이 변하게 된다.
얕은 복사는 의존적인 복사라고 생각하면 좋다.

깊은 복사란(Deep Copy)?

깊은 복사는 전체 복사로 생각하면 좋다. 얕은 복사와 달리 객체가 가진 모든 멤버를 복사하는 것을 말한다. 객체가 참조 타입의 멤버를 포함할 경우 참조값의 복사가 아닌 참조된 객체 자체가 복사되는 것을 말한다.
깊은 복사는 새로이 동적할당을 받고, 원본의 데이터를 복사한다.

예제코드

#include<iostream>
using namespace std;

class MyArray {
public:
	int size;
	int* data;

	MyArray(int size){
		this->size = size;
		data = new int[size];
	}
	//원래 있던 것을 지움.
	~MyArray() {
		if (this->data == NULL) {
			delete[]data;
		}
	}
	
    
	MyArray(const MyArray& other) {
		//모든걸 복사한 후 새로운 객체 생성
		this->size = other.size;
		this->data = new int[size];

		for (int i{ 0 }; i < other.size; i++) {
			this->data[i] = other.data[i];
		}
	}
};

int main() {

	MyArray buffer(10);
	buffer.data[0] = 2;

	MyArray clone = buffer;
	buffer.data[0] = 3;

	cout << "clone.data[0] = " << clone.data[0] << endl;




	return 0;
}

결과에서 볼 수 있듯이 복사한 대상자가 바뀌더라도 복사된 객체는 아무런 영향을 받지 않는다.
깊은 복사는 복사한 이후 완전 개별적인 객체가 된다. 이점이 얕은 복사와 가장 큰 차이점이다.

profile
집돌이 FE개발자의 노트

1개의 댓글

comment-user-thumbnail
2023년 9월 9일
//원래 있던 것을 지움.
~MyArray() {
	if (this->data == NULL) {
		delete[]data;
	}

이 부분에서 왜 data가 널일때만 delete 하는건가요?

답글 달기