복사생성자(Copy Constructor)
복사생성자
C++ 스타일의 초기화
int num = 20;
int &ref = num;int num(20);
int &ref(num);기본 복사생성자
#include <iostream>
using namespace std;
class Point {
int x;
int y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) { }
Point& setX(int x) {
this->x = x;
return *this;
}
Point& setY(int y) {
this->y = y;
return *this;
}
void show() {
cout << "[" << x << "," << y << "]" << endl;
}
};
int main() {
Point p1(10, 50);
Point p2 = p1; // 복사
p1.setX(15).setY(35);
cout << "p1 : ";
p1.show();
cout << "p2 : ";
p2.show();
return 0;
}깊은 복사와 얕은 복사
깊은복사 생성자 정의
Last updated