์ „์ฒด ๊ธ€

Python, C++, Data Science ๊ณต๋ถ€ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.
C++์—์„œ๋Š” ํด๋ž˜์Šค์—๋„ ์ƒ์ˆ˜ (const) ๊ฐœ๋…์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. const๊ฐ€ ์ ์šฉ๋œ ํด๋ž˜์Šค๋Š” ์†์„ฑ(attribute)๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์†์„ฑ์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? const Player villain {"Villain", 100, 55}; villain.set_name("Thanos");// COMPILER ERROR std::cout
this ํฌ์ธํ„ฐ๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— *this ๋กœ ์ ‘๊ทผํ•˜๋ฉด ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฃผ์†Œ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋Š” ํด๋ž˜์Šค ๋ฒ”์œ„ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (ํŒŒ์ด์ฌ์„ ๊ณต๋ถ€ํ•ด๋ณด์…จ๋‹ค๋ฉด self ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๊ฐํ•˜์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๋น ๋ฅผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.) ์˜ˆ์‹œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. void Account::set_balance(double balance){ this->balance = balance; } ์ž…๋ ฅ ์ธ์ž๋ฅผ balance_input ์™€ ๊ฐ™์ด ์„ค์ •ํ•˜๊ณ  balance = balance_input ์œผ๋กœ ํ•ด๋„ ๋˜์ง€๋งŒ, ์œ„์™€๊ฐ™์ด ๊ฐ์ฒด ์Šค์Šค๋กœ์˜ balance๋ฅผ this๋กœ ํ‘œ๊ธฐํ•˜์—ฌ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
INTRO int total {0}; total = 100 + 200; ์œ„ ์ฝ”๋“œ์—์„œ total์€ ํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜ ๊ฐ’, L-Value์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  total์— ํ• ๋‹น๋˜๋Š” 100+200 ์ˆ˜์‹์€ ํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋Š” R-Value์ž…๋‹ˆ๋‹ค. C++์—์„œ ์ด๋Ÿฐ ๊ฐ’๋“ค์€ ์ด๋ฆ„์—†๋Š” temp value๋กœ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋‹ค๊ฐ€, total๊ณผ ๊ฐ™์€ ๊ฐ’์— ๋ณต์‚ฌ ์ €์žฅ๋˜๊ณ  ๋‚œ ๋’ค ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ์ผ์ด temp value๋ฅผ temp object๋กœ๋งŒ ๋ฐ”๋€Œ์–ด์„œ ๊ฐ์ฒด์—์„œ๋„ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด temporary object๊ฐ€ ๊ทธ๋งŒํผ ๋งŽ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ๊ฐ์ฒด๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. Deep copy๋ฅผ ํ–‰ํ•˜๋ฉด ๋”์šฑ ๋งŽ์ด ์š”..
C++ Class ์˜ Copy Constructor์—๋Š” default copy constructor์ด ์‚ฌ์šฉํ•˜๋Š” Shallow Copying๊ณผ Deep Copying์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์–•์€ ๋ณต์‚ฌ, shallow copying์„ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์ง€๋งŒ, ์–•์€ ๋ณต์‚ฌ๊ฐ€ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ๊ฐ๊ฐ ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Shallow Copying - ์–•์€ ๋ณต์‚ฌ Shallow copy๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” default copy constructor๊ฐ€ ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. Shallow copying์€ ๋ณต์‚ฌ ๋Œ€์ƒ์˜ ๊ฐ์ฒด์˜ ๋ชจ๋“  ์†์„ฑ์„ ์ง€๋‹Œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ๋  ๋•Œ ์ž…๋‹ˆ๋‹ค. ๋ณต์‚ฌ์˜ ๊ณผ์ •์—์„œ ํฌ์ธํ„ฐ๋Š” ๋ณต์‚ฌ๋˜์ง€๋งŒ, ..
Chan Lee
Chan Code & DS ๐Ÿง‘‍๐Ÿ’ป๐Ÿ“Š