๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ์‘์šฉ

[C++]ํฌ์ธํ„ฐ์™€ ๋ ˆํผ๋Ÿฐ์Šค ์ฐจ์ด

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 1. 9.

 

 

C++์—์„œ ํฌ์ธํ„ฐ์™€ ๋ ˆํผ๋Ÿฐ์Šค๋Š” ๋ชจ๋‘ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฑฐ๋‚˜ ์ฐธ์กฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ๊ทธ๋“ค ๊ฐ„์— ์ค‘์š”ํ•œ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 


์ •์˜

- ํฌ์ธํ„ฐ(Pointer): ํฌ์ธํ„ฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ฐ„์ ‘ ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌ์ธํ„ฐ๋Š” '*' ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•˜๋ฉฐ, ํ• ๋‹น๋œ ์ฃผ์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ ˆํผ๋Ÿฐ์Šค(Reference): ๋ ˆํผ๋Ÿฐ์Šค๋Š” ๊ธฐ์กด ๋ณ€์ˆ˜์˜ ๋ณ„์นญ(alias)์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ์กด ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋ณ„์นญ์„ ๋งŒ๋“ค์–ด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆํผ๋Ÿฐ์Šค๋Š” '&' ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ๋˜๋ฉฐ, ํ•œ ๋ฒˆ ํ• ๋‹น๋˜๋ฉด ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 


์ดˆ๊ธฐํ™”

- ํฌ์ธํ„ฐ(Pointer): ํฌ์ธํ„ฐ๋Š” ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋„ ํฌ์ธํ„ฐ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ ํฌ์ธํ„ฐ๋Š” ์–ด๋–ค ์ฃผ์†Œ๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๋Š” ํŠน๋ณ„ํ•œ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

int num = 10;
int* ptr = # // ํฌ์ธํ„ฐ๋Š” num ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
int* nullPtr = nullptr; // ๋„ ํฌ์ธํ„ฐ๋กœ ์ดˆ๊ธฐํ™”


- ๋ ˆํผ๋Ÿฐ์Šค(Reference): ๋ ˆํผ๋Ÿฐ์Šค๋Š” ์„ ์–ธ๊ณผ ๋™์‹œ์— ๋ฐ˜๋“œ์‹œ ๊ธฐ์กด ๋ณ€์ˆ˜๋กœ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

int num = 10;
int& ref = num; // ๋ ˆํผ๋Ÿฐ์Šค๋Š” num ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

 



์‚ฌ์šฉ๋ฒ•

- ํฌ์ธํ„ฐ(Pointer): ํฌ์ธํ„ฐ๋Š” ๊ฐ„์ ‘ ์ฐธ์กฐ ์—ฐ์‚ฐ์ž '*'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ฝ๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

int num = 10;
int* ptr = #
*ptr = 20; // ptr์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ 20์œผ๋กœ ๋ณ€๊ฒฝ (num์€ ์ด์ œ 20)



๋ ˆํผ๋Ÿฐ์Šค(Reference): ๋ ˆํผ๋Ÿฐ์Šค๋Š” ๋ณ„๋„์˜ ์ฐธ์กฐ ์—ฐ์‚ฐ์ž๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ๊ธฐ์กด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

int num = 10;
int& ref = num;
ref = 20; // ref๋ฅผ ํ†ตํ•ด num ๋ณ€์ˆ˜์˜ ๊ฐ’์„ 20์œผ๋กœ ๋ณ€๊ฒฝ (num์€ ์ด์ œ 20)

 


๋„ ๊ฐ’(Null Value)

- ํฌ์ธํ„ฐ(Pointer): ํฌ์ธํ„ฐ๋Š” ๋„ ํฌ์ธํ„ฐ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์–ด๋–ค ์ฃผ์†Œ๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๋Š” ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

int* ptr = nullptr; // ํฌ์ธํ„ฐ๋Š” ์•„๋ฌด๊ฒƒ๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๋Š” ๋„ ํฌ์ธํ„ฐ์ž…๋‹ˆ๋‹ค.


๋ ˆํผ๋Ÿฐ์Šค(Reference): ๋ ˆํผ๋Ÿฐ์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•˜๋ฉฐ, ๋„ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ•ญ์ƒ ์œ ํšจํ•œ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 


Null ์ฒดํฌ


ํฌ์ธํ„ฐ(Pointer): ๋„ ํฌ์ธํ„ฐ์ธ์ง€ ํ™•์ธํ•˜์—ฌ ์•ˆ์ „ํ•œ ์ ‘๊ทผ์„ ๋ณด์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

int* ptr = nullptr;
if (ptr != nullptr) {
    // ptr์ด ์œ ํšจํ•œ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์„ ๋•Œ์˜ ์ฝ”๋“œ
}


๋ ˆํผ๋Ÿฐ์Šค(Reference): ๋ ˆํผ๋Ÿฐ์Šค๋Š” ์„ ์–ธ๊ณผ ๋™์‹œ์— ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™”๋˜๋ฏ€๋กœ ๋„ ์ฒดํฌ๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.



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

๋Œ“๊ธ€