C++/๊ธฐํƒ€

ยทC++/๊ธฐํƒ€
์ €๋ฒˆ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฃฌ ๋ณต์‚ฌ ๋ฐฉ์‹์˜ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์€ l-value์— ๋Œ€ํ•ด์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฃฐ ์ด๋™ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์€ r-value๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ ์กฐ๊ฐ์„ ์‚ดํŽด๋ด…์‹œ๋‹ค. Mystring s1; s1 = Mystring{"Ricky"};// Move Assignment ์—ฌ๊ธฐ์„œ empty string์„ ๋‹ด๋Š” Mystring ๊ฐ์ฒด s1์„ ์„ ์–ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  s1์— Mystring{"Ricky"}๋ฅผ ํ†ตํ•ด์„œ Ricky๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์†์„ฑ์œผ๋กœ ์ง€๋‹Œ Mystring ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, s1์— ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒˆ๋กœ์šด ์ด๋ฆ„ ์—†๋Š” ์ž„์‹œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ๊ฐ’์„ s1์— ์ €์žฅ์„ ํ•œ ๋’ค์— ์ž„์‹œ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด r-value ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„์‹œ๋‹ค์‹œํ”ผ ์ด๋Ÿฐ..
ยทC++/๊ธฐํƒ€
C++์—์„œ๋Š”, ํ•œ ๊ฐ์ฒด์—์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ์˜ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Mystring s1 {"Frank"}; Mystring s2 = s1;// NOT Assingment // Same as Mystring s2 {s1}; s2 = s1// Assignment C++ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ธฐ๋ณธ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์–•์€ ๋ณต์‚ฌ(shallow copy / memberwise assignment) ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์˜ ์†์„ฑ ์ค‘ raw pointer๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” ๊นŠ์€ ๋ณต์‚ฌ(deep copy)๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” c-style string๊ฐ’์˜ ํฌ์ธํ„ฐ๋ฅผ ์†์„ฑ์œผ๋กœ ์ง€๋‹ˆ๋Š” Mystring ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. class Mystring { private: char *st..
ยทC++/๊ธฐํƒ€
์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์ด๋ž€? C++ ์—์„œ ์šฐ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์™”์Šต๋‹ˆ๋‹ค. ๋”ํ•˜๊ธฐ(+), ๋นผ๊ธฐ(-), ๋‚˜๋ˆ„๊ธฐ(/), ๊ณฑํ•˜๊ธฐ(*)๊ฐ€ ๋Œ€ํ‘œ์ ์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž๋“ค๋„ ์ด๋ฏธ ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๋˜์–ด ์žˆ๋Š” ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ์˜ค๋ฒ„๋กœ๋”ฉ์ด ํ•œ ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜• ๊ฐ’์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ˜๋ณต์ ์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด์˜€์ฃ ? ์œ„์˜ ์ผ๋ฐ˜์  ์—ฐ์‚ฐ์ž๋“ค๋„ integer, double, float ๋“ฑ์˜ ์ž๋ฃŒํ˜•์„ ๋ชจ๋‘ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋Œ์•„์™€์„œ ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์„ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๋•Œ์—๋Š”, ์ฃผ๋กœ user defined type๋“ค์„ built-in type๋“ค๊ณผ ๋น„์Šทํ•˜๊ฒŒ ํ–‰๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์€ ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ ์ž๋™์ ์œผ๋กœ ํ–‰ํ•ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ์ข…..
ยทC++/๊ธฐํƒ€
cin.ignore()์€ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, cin์œผ๋กœ 1000 5000์„ ๋ฐ›์•˜์„ ๋•Œ ๋‹ค์Œ cin์€ ์ž…๋ ฅ์„ ํ•˜์ง€๋„ ์•Š์•˜๋Š”๋ฐ 5000 ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋ฒ„ํผ์— 5000 ๊ฐ’์ด ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ cin.ignore()์œผ๋กœ ๋ฒ„ํผ๋ฅผ ๋น„์šฐ๊ณ  ๋‚˜์„œ ๋‹ค์‹œ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐฉ์ง€๋ฉ๋‹ˆ๋‹ค.
Chan Lee
'C++/๊ธฐํƒ€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก