๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
C++

[C++ ์‹œํ—˜๋Œ€๋น„] 5์ฃผ์ฐจ - ํ•จ์ˆ˜์˜ ์ฐธ์กฐ, ๋ณต์‚ฌ์ƒ์„ฑ์ž

by Meaning_ 2022. 10. 2.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์ •๋ฆฌ

 

- ๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜

: ๊ฐ’๊ณผ ์ฃผ์†Œ๊ฐ’ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ผ๊ณ  ์šฐ์„  ์ƒ๊ฐํ•˜๊ธฐ

 

- ๋™์ ํ• ๋‹น

: ๋ง ๊ทธ๋Œ€๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ณ  ๋‚˜์„œ ํ• ๋‹น 

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒ ์ฅฌ?

 

- ๋ณต์‚ฌ์ƒ์„ฑ์ž

 

์˜ˆ๋ฅผ ๋“ค์–ด Person p1=new Person();

Person p2;

p1=p2; ๊ฐ™์ด ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค? ๊ทธ๋Ÿฌ๋ฉด ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์„ฑ๋จ 

์šฐ๋ฆฌ๋„ ๋ชจ๋ฅด๋Š” ์‚ฌ์ด์— ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š”๊ฑฐ

๊ทผ๋ฐ ์ด ๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋Š” ์–•์€ ๋ณต์‚ฌ์ด๊ธฐ์— ๊ฐ’๋„ ๋ณต์‚ฌํ•˜๊ณ  ์ฃผ์†Œ๊ฐ’ ๊ณต๊ฐ„์„ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด 

๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊นŠ์€ ๋ณต์‚ฌ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•จ!

 

ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ฐœ๋…์ •๋ฆฌ ๋ ๋ณธ๋ก ์œผ๋กœ ๋“ค์–ด๊ฐ€์ž!

 

๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ฐ์ฒด ์ „๋‹ฌํ•˜๊ธฐ

 

๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด ๊ณต๊ฐ„์ด ์Šคํƒ์— ํ• ๋‹น๋œ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด๊ฐ€ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ๋œ๋‹ค -> ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค!

 

์™œ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š์„๊นŒ? 

=> ํ˜ธ์ถœ๋˜๋Š” ์ˆœ๊ฐ„ ์‹ค์ธ์ž ๊ฐ์ฒด ์ƒํƒœ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด์— ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•จ!

 

#include <iostream>
using namespace std;

class Circle {
private:
	int radius; 
public:
	Circle(); 
	Circle(int r);
	~Circle();
	double getArea()  { return 3.14*radius*radius; }
	int getRadius() { return radius; }
	void setRadius(int radius) { this->radius = radius 
}; 

Circle::Circle() {
	radius = 1;
	cout << "์ƒ์„ฑ์ž ์‹คํ–‰ radius = " << radius << endl;
}

Circle::Circle(int radius) {
	this->radius = radius;
	cout << "์ƒ์„ฑ์ž ์‹คํ–‰ radius = "  << radius << endl;
}

Circle::~Circle() {
	cout << "์†Œ๋ฉธ์ž ์‹คํ–‰ radius = " << radius << endl;
}
void increase(Circle c) {
	int r = c.getRadius();
	c.setRadius(r+1);
}

int main() {
	Circle waffle(30);
	increase(waffle);
	cout << waffle.getRadius() << endl;
}

 

 

๋งค๊ฐœ๋ณ€์ˆ˜ c์˜ ์ƒ์„ฑ์ž๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค!  ๋‹จ ํ•จ์ˆ˜ ๋น ์ ธ๋‚˜๊ฐˆ๋•Œ c์— ๋Œ€ํ•œ ์†Œ๋ฉธ์ž๊ฐ€ ์‹คํ–‰๋จ

 

waffle๊ณผ c๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ณต๊ฐ„์— ์žˆ๋‹ค.

 

 

 

 

์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ๋กœ ๊ฐ์ฒด ์ „๋‹ฌํ•˜๊ธฐ

 

๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ

ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ƒ์„ฑ์ž ์†Œ๋ฉธ์ž๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๊ตฌ์กฐ 

 

int main(){
	Circle waffle(30);
    increase(&waffle);

}

void increase(Circle *c) {
	int r = c->getRadius();
	c.setRadius(r+1);
}

 

waffle์˜ ์ฃผ์†Œ๊ฐ€ c์— ์ „๋‹ฌ๋œ๋‹ค. ์•ž์„  ๊ฒฝ์šฐ์™€ ๋‹ค๋ฅด๊ฒŒ waffle๊ณผ c๊ฐ€ ๊ฐ™์€ ๊ณต๊ฐ„์„ ๊ณต์œ ํ•œ๋‹ค.

 

 

์ฐธ์กฐ๋ž€?

 

์ฐธ์กฐ๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋‚˜ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. 

์ด๋ฆ„๋งŒ ์ƒ๊ธฐ๋ฉฐ ์ƒˆ๋กœ์šด ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

int n=2;

int &refn=n; 

 

์•„๋ฌดํŠผ ์ง€๊ธˆ ์ƒํ™ฉ์—์„œ๋Š” ๊ฐ’๊ณผ ์ฃผ์†Œ๊ฐ’ ๊ฐ€์ง„๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋‘์ž. 

 

n=4;

refn++;

์ด๋ผ๋ฉด n์€ ๋ญ˜๊นŒ? 5๊ฐ€ ๋ ๊ฒƒ์ด๋‹ค. refn๋„ 5๊ฐ€๋œ๋‹ค.

 

int* p=&refn; 

*p=20;

์ด๋ผ๋ฉด ref์€ ๋ญ˜๊นŒ? 20์ด๋‹ค!! ๊ทธ๋Ÿฌ๋ฉด n์€ ๋ญ˜๊นŒ? ๋‘˜์ด ์ฃผ์†Œ๊ฐ’ ๊ณต์œ ํ•˜๊ณ  ์žˆ์œผ๋‹ˆ 20์ด๋‹ค. 

 

 

 

์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ

 

ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.

์ฐธ์กฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„๋งŒ ์ƒ๊ธฐ๊ณ  ๊ณต๊ฐ„์€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

์‹ค์ธ์ž ๋ณ€์ˆ˜ ๊ณต๊ฐ„์„ ๊ณต์œ ํ•œ๋‹ค! (์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ๋„ ์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ ์ฒ˜๋Ÿผ swap์ด ๋  ๊ฒƒ )

 

 

 

์ฐธ์กฐ๋ฅผ ์“ด๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ increase(Circle &c)

ํฌ์ธํ„ฐ ์“ด๋‹ค๋ฉด increase(Circle* c)

 

 

#include <iostream>
using namespace std;

class Circle {
	int radius;
public:
	Circle() { radius = 1; }
	Circle(int radius) { this->radius = radius; }
	void setRadius(int radius) { this->radius = radius; }
	double getArea() { return 3.14*radius*radius; }
};

int main() {
	Circle donut;
	readRadius(donut);
	cout << "donut์˜ ๋ฉด์  = " <<donut.getArea() << endl;
}

๊ทธ๋ƒฅ readRadius์— ๊ทธ๋ƒฅ donut๋งŒ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ์ผ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ 

 

void readRadius(Circle &c)

 

 

๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š”? ์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š”? 

 

char c = 'a';

char get() { // char ๋ฆฌํ„ด
	return c; // ๋ณ€์ˆ˜ c์˜ ๋ฌธ์ž(‘a’) ๋ฆฌํ„ด
}

char a = get(); // a = 'a'๊ฐ€ ๋จ

get() = 'b'; // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜

 

char get()์ด๋ฉด c ์•ˆ์˜ ๋ฌธ์ž์ธ 'a'๋ฆฌํ„ด์„ ํ•œ๋‹ค. 'a'='b'์ผ์ˆ˜ ์—†๊ธฐ์— ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋œฌ๋‹ค.

 

char c = 'a';

char& find() { // char ํƒ€์ž…์˜ ์ฐธ์กฐ ๋ฆฌํ„ด
	return c; // ๋ณ€์ˆ˜ c์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋ฆฌํ„ด 
}

char a = find(); // a = 'a'๊ฐ€ ๋จ

char &ref = find(); //  ref๋Š” c์— ๋Œ€ํ•œ ์ฐธ์กฐ
ref = 'M'; // c= 'M'

find() = 'b'; // c = 'b'๊ฐ€ ๋จ

char& get()์ด๋ฉด charํƒ€์ž…์˜ ๊ณต๊ฐ„์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 

์ฆ‰, find()๊ฐ€ ๋ฆฌํ„ดํ•œ ๊ณต๊ฐ„์— ref๊ฐ€ ๋“ค์–ด๊ฐ€๊ธฐ์— ref='M'์€ c='M'์ด ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๋ฟ๋งŒ ์•„๋‹ˆ๋ผ find()='b'๋„ c='b'๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. 

 

์ฆ‰, ์ฐธ์กฐ๋ฆฌํ„ด์€ ๊ฐ’์˜ ๋ฐ˜ํ™˜์ด ์•„๋‹Œ ๊ณต๊ฐ„์˜ ๋ฐ˜ํ™˜์ด๋‹ค. 

 

#include <iostream>
using namespace std;

char& find(char s[], int index) {
	return s[index]; // ์ฐธ์กฐ ๋ฆฌํ„ด
}

int main() {
	char name[] = "Mike";
	cout << name << endl;

	find(name, 0) = 'S'; // name[0]='S'๋กœ ๋ณ€๊ฒฝ
	cout << name << endl;

	char& ref = find(name, 2); 
	ref = 't'; // name = "Site"
	cout << name << endl;
}

 

์—ฌ๊ธฐ์„œ find(name,0)์„ ๋ณด๋ฉด name[0]='S'๊ฐ€ ๋œ๋‹ค!

 

 

๊นŠ์€๋ณต์‚ฌ์™€ ์–•์€ ๋ณต์‚ฌ 

 

โญ ํ•œ์ค„ ์ •๋ฆฌ

 

์–•์€๋ณต์‚ฌ๋Š” ๊ณต๊ฐ„์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ฃผ์†Œ๊ฐ’์„ ๊ณต์œ ํ•œ๋‹ค.

๊นŠ์€ ๋ณต์‚ฌ๋Š” ๊ณต๊ฐ„์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด์„œ ์ฃผ์†Œ๊ฐ’๊ณผ ๊ฐ’์„ ๋ณต์‚ฌํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณต๊ฐ„์— ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. 

 

 

๐ŸŽ€ ppt์ƒ์—์„œ ์ฃผ์ ˆ์ฃผ์ ˆ

 

¨์–•์€ ๋ณต์‚ฌ(shallow copy)
¤๊ฐ์ฒด ๋ณต์‚ฌ ์‹œ, ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„๋ฅผ 1:1๋กœ ๋ณต์‚ฌ
¤๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ๋™์  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋œ ๊ฒฝ์šฐ
n์‚ฌ๋ณธ์€ ์›๋ณธ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น ๋ฐ›์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ
¨๊นŠ์€ ๋ณต์‚ฌ(deep copy)
¤๊ฐ์ฒด ๋ณต์‚ฌ ์‹œ, ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„๋ฅผ 1:1๋Œ€๋กœ ๋ณต์‚ฌ
¤๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ๋™์  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋œ ๊ฒฝ์šฐ
n์‚ฌ๋ณธ์€ ์›๋ณธ์ด ๊ฐ€์ง„ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ ๋งŒํผ ๋ณ„๋„๋กœ ๋™์  ํ• ๋‹น
n์›๋ณธ์˜ ๋™์  ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์‚ฌ๋ณธ์— ๋ณต์‚ฌ
¤์™„์ „ํ•œ ํ˜•ํƒœ์˜ ๋ณต์‚ฌ
n์‚ฌ๋ณธ๊ณผ ์›๋ณธ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฌธ์ œ ์—†์Œ

 

 

๋ณต์‚ฌ์ƒ์„ฑ์ž

 

๊ฐ์ฒด๊ฐ€ ๋ณต์‚ฌ ์ƒ์„ฑ๋  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํŠน๋ณ„ํ•œ ์ƒ์„ฑ์ž. 

class Circle{

	Circle(const Circle& c); //๋ณต์‚ฌ์ƒ์„ฑ์ž ์„ ์–ธ
    
 }
 
 Circle::Circle(const Circle& c); //๋ณต์‚ฌ ์ƒ์„ฑ์ž ๊ตฌํ˜„

 

๐ŸŽ€ ํŠน์ง•

๋ณต์‚ฌ์ƒ์„ฑ์ž๋Š” ํ•œ ํด๋ž˜์Šค์— ์˜ค์ง ํ•œ๊ฐœ๋งŒ ์„ ์–ธ๊ฐ€๋Šฅ

๋ณต์‚ฌ์ƒ์„ฑ์ž๋Š” ๋ณดํ†ต ์ƒ์„ฑ์ž์™€ ํด๋ž˜์Šค ๋‚ด์—์„œ ์ค‘๋ณต ์„ ์–ธ ๊ฐ€๋Šฅํ•˜๋‹ค! 

ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ๋…ํŠนํ•œ ์ƒ์„ฑ์ž.

 

#include <iostream>
using namespace std;

class Circle {
private:
	int radius; 
public:
	Circle(const Circle& c); // ๋ณต์‚ฌ ์ƒ์„ฑ์ž ์„ ์–ธ
	Circle() { radius = 1; }
	Circle(int radius) { this->radius = radius; }
	double getArea() { return 3.14*radius*radius; }
}; 

Circle::Circle(const Circle& c) { // ๋ณต์‚ฌ ์ƒ์„ฑ์ž ๊ตฌํ˜„
	this->radius = c.radius;
	cout << "๋ณต์‚ฌ ์ƒ์„ฑ์ž ์‹คํ–‰ radius = " << radius << endl;
}

int main() {
	Circle src(30); // src ๊ฐ์ฒด์˜  ๋ณดํ†ต ์ƒ์„ฑ์ž ํ˜ธ์ถœ
	Circle dest(src); // dest ๊ฐ์ฒด์˜ ๋ณต์‚ฌ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

	cout << "์›๋ณธ์˜ ๋ฉด์  = " << src.getArea() << endl;
	cout << "์‚ฌ๋ณธ์˜ ๋ฉด์  = " << dest.getArea() << endl;
}

 

Circle src(30)์„ ํ•˜๋ฉด Circle(int radius) ํ˜ธ์ถœ๋จ

Circle dest(src) ํ•˜๋ฉด Circle(const Circle& c) ํ˜ธ์ถœ๋จ

 

 

๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž

 

Circle::Circle(const Circle& c) {

  this->radius = c.radius;

  // ์›๋ณธ ๊ฐ์ฒด c์˜ ๊ฐ ๋ฉค๋ฒ„๋ฅผ ์‚ฌ๋ณธ(this)์— ๋ณต์‚ฌํ•œ๋‹ค.

}

 

์ด๋Ÿฐ ํ˜•์‹์ด ๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž์ด๋‹ค.

๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์„ ์–ธ๋˜์ง€ ์•Š์€ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

 

 

 

์–•์€๋ณต์‚ฌ์˜ ๋ฌธ์ œ์ 

 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

class Person { // Person ํด๋ž˜์Šค ์„ ์–ธ
	char* name;
	int id;
public:
	Person(int id, const char* name); // ์ƒ์„ฑ์ž
	~Person(); // ์†Œ๋ฉธ์ž
	void changeName(const char *name);
	void show() { cout << id << ',' << name << endl; }
};

Person::Person(int id, const char* name) { // ์ƒ์„ฑ์ž
	this->id = id;
	int len = strlen(name); // name์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜
	this->name = new char [len+1]; // name ๋ฌธ์ž์—ด ๊ณต๊ฐ„ ํ•Ÿ๋‹น
	strcpy(this->name, name); // name์— ๋ฌธ์ž์—ด ๋ณต์‚ฌ
}

Person::~Person() {// ์†Œ๋ฉธ์ž
	if(name) // ๋งŒ์ผ name์— ๋™์  ํ• ๋‹น๋œ ๋ฐฐ์—ด์ด ์žˆ์œผ๋ฉด
		delete [] name; // ๋™์  ํ• ๋‹น ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋ฉธ
}

void Person::changeName(const char* name) { // ์ด๋ฆ„ ๋ณ€๊ฒฝ
	if(strlen(name) > strlen(this->name))
		return; 
	strcpy(this->name, name);
}
int main() {
	Person father(1, "Kitae");			// (1) father ๊ฐ์ฒด ์ƒ์„ฑ
	Person daughter(father);			// (2) daughter ๊ฐ์ฒด ๋ณต์‚ฌ ์ƒ์„ฑ. ๋ณต์‚ฌ์ƒ์„ฑ์žํ˜ธ์ถœ

	cout << "daughter ๊ฐ์ฒด ์ƒ์„ฑ ์งํ›„ ----" << endl;
	father.show();						// (3) father ๊ฐ์ฒด ์ถœ๋ ฅ
	daughter.show();					// (3) daughter ๊ฐ์ฒด ์ถœ๋ ฅ

	daughter.changeName("Grace"); // (4) daughter์˜ ์ด๋ฆ„์„ "Grace"๋กœ ๋ณ€๊ฒฝ
	cout << "daughter ์ด๋ฆ„์„ Grace๋กœ ๋ณ€๊ฒฝํ•œ ํ›„ ----" << endl;
	father.show();						// (5) father ๊ฐ์ฒด ์ถœ๋ ฅ
	daughter.show();					// (5) daughter ๊ฐ์ฒด ์ถœ๋ ฅ

	return 0;								// (6), (7) daughter, father ๊ฐ์ฒด ์†Œ๋ฉธ
}

 

Person father(1,"kitae")๋ฅผ ํ•˜๋ฉด Person(int id,const char* name) ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

Person daughter(father); ๋ฅผ ํ•˜๋ฉด ๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๋Š”๋ฐ

๋””ํดํŠธ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ๋ณด๋ฉด

 

Person::Person(const Person& p) {
	this->id = p.id;
	this->name = p.name;
}

 

์š”๊ฒŒ ํ˜ธ์ถœ๋œ๋‹ค.

 

๊ทผ๋ฐ ๋ฌธ์ œ๋Š” daughter์™€ father๊ฐ€ ์–•์€๋ณต์‚ฌ๊ฐ€ ๋˜๋ฉด์„œ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค!

 

daughter,father์ˆœ์œผ๋กœ ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋œ๋‹ค. ๊ทผ๋ฐ ๋ฌธ์ œ์ ์ด  ๋‘˜์ด ๊ฐ™์€ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋‹ˆ๊นŒ

daughter๊ฐ€ ์†Œ๋ฉธ๋˜๊ณ  ๋‚˜๋ฉด father๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๊ฐ’์€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค?! ์ƒํ™ฉ์ด ๋˜๋ฉด์„œ

ํ”„๋กœ๊ทธ๋žจ์ด ๋น„์ •์ƒ ์ข…๋ฃŒ๋œ๋‹ค.

 

์†Œ๋ฉธ์ž์—์„œ daugther ๊ฐ์ฒด์†Œ๋ฉธ๋˜๋ฉด์„œ ์ด๋ฏธ name์ด ๋‚ ์•„๊ฐ€๋ฒ„๋ ธ๋Š”๋ฐ

father์—์„œ ์ด๋ฏธ ๋‚ ์•„๊ฐ€๋ฒ„๋ฆฐ์•  ๋‚ ๋ฆฌ๋ ค๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ๊ฒƒ์ด๋‹ค..!

 

๊ทธ๋Ÿผ ํ•ด๊ฒฐ์ฑ…์€ ๋ฌด์—‡์ผ๊นŒ? ๋ฐ”๋กœ ๊นŠ์€๋ณต์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

์ด๋ฆ„์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๊ณต๊ฐ„ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.  this-> new char[len+1] ์ด๋Ÿฐ๋ฐฉ์‹์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๊ณต๊ฐ„์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  strcpy(this->name, name); ๋ฅผ ํ•˜๋ฉด์„œ name์— ๋ฌธ์ž์—ด์„ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ž ๊ทผ๋ฐ, ์ง€๊ธˆ๊นŒ์ง€  Person ํด๋ž˜์Šค๋ฅผ ๋ดค์„ ๋•Œ ๊ฐ์ฒด๋ฅผ ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์—†๋‹ค..!

๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋Š” ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” int id,const char* name์ด๋‹ค. 

๊ทผ๋ฐ ์šฐ๋ฆฌ๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์™€์•ผ ํ•˜๋Š” ๊ฑด ๊ฐ์ฒด์ด๋‹ค..!!

 

๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์„ ํ•ด์•ผํ•œ๋‹ค!!

๋ฐ”๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ์ด๋‹ค!!

 

Person::Person(const Person& person) { // ๋ณต์‚ฌ ์ƒ์„ฑ์ž
	this->id = person.id; // id ๊ฐ’ ๋ณต์‚ฌ
	int len = strlen(person.name);// name์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜
	this->name = new char [len+1]; // name์„ ์œ„ํ•œ ๊ณต๊ฐ„ ํ•Ÿ๋‹น
	strcpy(this->name, person.name); // name์˜ ๋ฌธ์ž์—ด ๋ณต์‚ฌ
	cout << "๋ณต์‚ฌ ์ƒ์„ฑ์ž ์‹คํ–‰. ์›๋ณธ ๊ฐ์ฒด์˜ ์ด๋ฆ„ " << this->name << endl;
}

 

father์™€ daughter๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ์— daughter๊ฐ€ ๋จผ์ € ์†Œ๋ฉธ์ž ํ˜ธ์ถœ๋˜์–ด๋„

father์—๋Š” ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๋Š”๋‹ค!!

 

 

๐ŸŽ€ ๋” ์ฐพ์•„๋ณธ ๋ฐ”๋กœ๋Š” 

 

๊นŠ์€ ๋ณต์‚ฌ(Deep Copy)๋Š” '์‹ค์ œ ๊ฐ’'์„ ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฉฐ,

์–•์€ ๋ณต์‚ฌ(Shallow Copy)๋Š” '์ฃผ์†Œ ๊ฐ’'์„ ๋ณต์‚ฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

 

์–•์€ ๋ณต์‚ฌ๋Š” ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์ธ ๋ฐ˜๋ฉด, ๊นŠ์€ ๋ณต์‚ฌ๋Š” ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์žฌ์ •์˜ํ•จ์œผ๋กœ์จ ์›ํ•˜๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋งŒ ๋ณต์‚ฌ๋ฅผ ํ•ด์ฃผ๋Š” ํ˜•์‹์ด๋‹ค. ๊ทธ๋ž˜์„œ ์–•์€ ๋ณต์‚ฌ์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 ์–•์€ ๋ณต์‚ฌ๋Š” ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋กœ ๋Œ€์ถฉ ๋ณต์‚ฌํ•˜๋Š” ๊ฐ์ด๋ผ๋ฉด, ๊นŠ์€ ๋ณต์‚ฌ๋Š” ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์žฌ์ •์˜ํ•ด์คŒ์œผ๋กœ์จ ๊ฐ์ฒด๊ฐ„์˜ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ์ฐธ์กฐ๋ฅผ ๋ง‰์•„์ฃผ๊ธฐ์— ๊นŠ์€ ๋ณต์‚ฌ๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

* ๋ณต์‚ฌ ์ƒ์„ฑ์ž์— const๋กœ ์ธ์ž๋ฅผ ๋ฐ›์•„์˜ค๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” ๊ธฐ์กด์˜ ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  ๊ทธ๋ž˜์„œ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์— const๋กœ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

 

https://musket-ade.tistory.com/30

 

[ C++ ] ๊นŠ์€ ๋ณต์‚ฌ( deep copy )์™€ ์–•์€ ๋ณต์‚ฌ ( shallow copy )

์–•์€ ๋ณต์‚ฌ( shallow copy )๋Š” ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์— ์˜ํ•œ ๋ฉค๋ฒ„ ๋Œ€ ๋ฉค๋ฒ„์˜ ๋ณต์‚ฌ ๋ฐฉ์‹์„ ๋งํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ํž™์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋ณด์ž. Aํ˜• ๊ฐ์ฒด a์™€ b

musket-ade.tistory.com

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€