일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- react native typescript
- html
- c++ using
- 스탠실 버퍼 튜토리얼
- 싸이월드
- C++
- react native ios 기기 연결
- CSS
- javascript
- c++ 정보은닉
- 리액트 네이티브 설치 오류
- react
- react native
- 스탠실 버퍼 시작
- react native accessible
- react native typescript navigate
- unity stencil buffer
- GitHub
- Expo
- react native typescript navigation
- react native 타입스크립트
- 스탠실 버퍼 사용
- react native mac
- 벡터와 리스트의 차이
- node.js
- 리액트 네이티브 맥
- stencil buffer
- cyworld
- node
- react-native
Archives
- Today
- Total
혀니의 이거저거 뿌시기
얕은복사 / 깊은복사 본문
728x90
* 얕은복사: 의존적인 복사
#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;
}
복사한 대상자가 바뀌면 복사한 객체도 같이 변하게 됨
*깊은복사: 객체가 가진 모든 멤버를 복사
새로이 동적할당을 받고, 원본의 데이터를 복사함
#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;
}
-> 복사한 대상자가 바껴도 복사된 객체는 아무런 영향을 받지 않음
728x90
'CS > C++' 카테고리의 다른 글
이동 의미론 ( move semantics) (0) | 2023.12.14 |
---|---|
L value 와 R value (0) | 2023.12.14 |
c++ typedef vs using 키워드 차이점 (0) | 2023.12.14 |
C++ template 장단점 (0) | 2023.12.14 |
c++ 캐스팅 (0) | 2023.12.14 |