C Programming Language/C++

C++ μ§€μ—­λ³€μˆ˜μ™€ μ „μ—­λ³€μˆ˜μ— λŒ€ν•΄ ν•™μŠ΅ν•˜κΈ°

567Rabbit 2024. 8. 14. 17:54

μ§€μ—­λ³€μˆ˜μ™€ μ „μ—­λ³€μˆ˜μ˜ μ •μ˜μ™€ μœ„μΉ˜

 

- μ „μ—­ λ³€μˆ˜λŠ” ν•¨μˆ˜ λ°–μ—μ„œ μ •μ˜λ˜λ©°, ν”„λ‘œκ·Έλž¨μ˜ λͺ¨λ“  ν•¨μˆ˜μ—μ„œ μ ‘κ·Όν•  수 μžˆλ‹€.

- μ§€μ—­ λ³€μˆ˜λŠ” ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ •μ˜λ˜λ©°, ν•΄λ‹Ή ν•¨μˆ˜ λ‚΄μ—μ„œλ§Œ μœ νš¨ν•˜λ‹€.

 

 

cpp
λ‹«κΈ°
#include <iostream> using namespace std; int global_variable = 30; //μ „μ—­λ³€μˆ˜ μ •μ˜ void exampleFunction() { ​​​​int local_variable = 15; //μ§€μ—­λ³€μˆ˜ μ •μ˜ ​​​​cout << "μ „μ—­ λ³€μˆ˜: " << global_variable << endl; ​​​​cout << "μ§€μ—­ λ³€μˆ˜: " << local_variable << endl; } int main() { ​​​​exampleFunction(); ​​​​// 좜λ ₯: ​​​​// μ „μ—­ λ³€μˆ˜: 30 ​​​​// μ§€μ—­ λ³€μˆ˜: 15 ​​​​// λ‹€μŒ μ½”λ“œλŠ” 컴파일 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€ ​​​​// cout << local_variable << endl; // 'local_variable' was not declared in this scope ​​​​return 0; }

 

- μ „μ—­λ³€μˆ˜λ₯Ό μ΄ˆκΈ°ν™”ν•˜μ§€ μ•ŠμœΌλ©΄ 0, false, 0.0F와 같이 μžλ™μœΌλ‘œ μ΄ˆκΈ°κ°’μ΄ μ‚¬μš©λ˜μ§€λ§Œ μ§€μ—­λ³€μˆ˜μ˜ 값은 μžλ™μœΌλ‘œ μ΄ˆκΈ°ν™”λ˜μ§€ μ•ŠμœΌλ―€λ‘œ μ§€μ—­ λ³€μˆ˜λŠ” λ³€μˆ˜λ₯Ό 선언함과 λ™μ‹œμ— μ΄ˆκΈ°κ°’μ„ μ„€μ •ν•΄μ•Ό ν•œλ‹€. μ΄ˆκΈ°κ°’을 μ„€μ •ν•˜μ§€ μ•Šμ€ μ§€μ—­ λ³€μˆ˜λŠ” garbage 값이 좜λ ₯될 수 μžˆλ‹€.

 

 

 

< μ „μ—­ λ³€μˆ˜μ™€ μ§€μ—­ λ³€μˆ˜μ˜ 이름 좩돌 >

 

- λ§Œμ•½ μ „μ—­λ³€μˆ˜μ—μ„œ μ •μ˜ν•œ λ³€μˆ˜λ₯Ό μ§€μ—­λ³€μˆ˜μ—μ„œ λ‹€μ‹œ μ •μ˜ν•œλ‹€λ©΄, μ§€μ—­ λ³€μˆ˜κ°€ μš°μ„ μ μœΌλ‘œ μ‚¬μš©λœλ‹€.

- μ „μ—­ λ³€μˆ˜λŠ” μ§€μ—­ λ³€μˆ˜λ‘œ 인해 κ°€λ €μ§€λŠ” ν˜„μƒμ„ μ˜μ—­μ˜ κ°€λ¦Ό(hiding)이라고 ν•œλ‹€.

cpp
λ‹«κΈ°
#include <iostream> using namespace std; // μ „μ—­ λ³€μˆ˜ μ •μ˜ int value = 100; void exampleFunction() { ​​​​// μ§€μ—­ λ³€μˆ˜ μ •μ˜ ​​​​int value = 50; ​​​​cout << "μ§€μ—­ λ³€μˆ˜ value: " << value << endl; } int main() { ​​​​cout << "μ „μ—­ λ³€μˆ˜ value: " << value << endl; // μ „μ—­ λ³€μˆ˜ value: 100 ​​​​exampleFunction(); // μ§€μ—­ λ³€μˆ˜ value: 50 ​​​​return 0; }

 

- C++ μ–Έμ–΄μ—μ„œλŠ” μ˜μ—­ μ—°μ‚°μž 즉 '::'λ₯Ό μ΄μš©ν•΄μ„œ μ „μ—­ λ³€μˆ˜λ₯Ό 항상 지칭이 κ°€λŠ₯ν•˜λ‹€.

- 근본적인 것은 μ‹€μ œλ‘œ μ „μ—­ λ³€μˆ˜ 및 μ§€μ—­ λ³€μˆ˜κ°€ λ™μΌν•œ 이름을 κ°€μ§€μ§€ μ•Šλ„λ‘ λ³€μˆ˜κ°€ μ €μž₯ν•˜λŠ” 정보λ₯Ό μ •ν™•ν•˜κ²Œ λœ»ν•˜λŠ” μš©μ–΄λ₯Ό λ³€μˆ˜ μ΄λ¦„μœΌλ‘œ μ‚¬μš©ν•˜λ©°, μ κ²€ν•˜λŠ” 것이닀,

- λŒ€κ·œλͺ¨ ν”„λ‘œκ·Έλž¨μ—μ„œλŠ” λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•΄μ„œ μ „μ—­ λ³€μˆ˜/ ν•¨μˆ˜ 등을 μ²΄κ³„μ μœΌλ‘œ κ΄€λ¦¬ν•¨μœΌλ‘œμ¨ 이름 좩돌 문제λ₯Ό νšŒν”Όν•œλ‹€.

 

cpp
λ‹«κΈ°
#include <iostream> using namespace std; // μ „μ—­ λ³€μˆ˜ μ •μ˜ int value = 100; void exampleFunction() { ​​​​int value = 50; // μ§€μ—­ λ³€μˆ˜ μ •μ˜ ​​​​cout << "μ§€μ—­ λ³€μˆ˜ value: " << value << endl; // μ§€μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​cout << "μ „μ—­ λ³€μˆ˜ value: " << ::value << endl; // μ „μ—­ λ³€μˆ˜ μ‚¬μš© } int main() { ​​​​cout << "μ „μ—­ λ³€μˆ˜ value: " << value << endl; // μ „μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​exampleFunction(); // ν•¨μˆ˜ 호좜 ​​​​return 0; }

 

좜λ ₯ :

μ „μ—­ λ³€μˆ˜ value: 100

μ§€μ—­ λ³€μˆ˜ value: 50

μ „μ—­ λ³€μˆ˜ value: 100

 

- μ—¬κΈ°μ„œ ::valueλŠ” μ „μ—­ λ³€μˆ˜ valueλ₯Ό μ§€μΉ­ν•œλ‹€. exampleFunction ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλŠ” μ§€μ—­ λ³€μˆ˜ valueκ°€ μš°μ„ μ μœΌλ‘œ μ‚¬μš©λ˜μ§€λ§Œ, ::valueλ₯Ό 톡해 μ „μ—­ λ³€μˆ˜μ— μ ‘κ·Όν•  수 μžˆλ‹€.

 

cpp
λ‹«κΈ°
#include <iostream> using namespace std; namespace NamespaceA { ​​​​int value = 100; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ A의 μ „μ—­ λ³€μˆ˜ } namespace NamespaceB { ​​​​int value = 200; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ B의 μ „μ—­ λ³€μˆ˜ } void exampleFunction() { ​​​​int value = 50; // μ§€μ—­ λ³€μˆ˜ ​​​​cout << "μ§€μ—­ λ³€μˆ˜ value: " << value << endl; // μ§€μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​cout << "NamespaceA의 μ „μ—­ λ³€μˆ˜ value: " << NamespaceA::value << endl; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ A의 μ „μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​cout << "NamespaceB의 μ „μ—­ λ³€μˆ˜ value: " << NamespaceB::value << endl; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ B의 μ „μ—­ λ³€μˆ˜ μ‚¬μš© } int main() { ​​​​cout << "NamespaceA의 μ „μ—­ λ³€μˆ˜ value: " << NamespaceA::value << endl; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ A의 μ „μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​cout << "NamespaceB의 μ „μ—­ λ³€μˆ˜ value: " << NamespaceB::value << endl; // λ„€μž„μŠ€νŽ˜μ΄μŠ€ B의 μ „μ—­ λ³€μˆ˜ μ‚¬μš© ​​​​exampleFunction(); // ν•¨μˆ˜ 호좜 ​​​​return 0; }

 

좜λ ₯ : 

NamespaceA의 μ „μ—­ λ³€μˆ˜ value: 100

NamespaceB의 μ „μ—­ λ³€μˆ˜ value: 200

μ§€μ—­ λ³€μˆ˜ value: 50

NamespaceA의 μ „μ—­ λ³€μˆ˜ value: 100

NamespaceB의 μ „μ—­ λ³€μˆ˜ value: 200

 

 

 

 

정적 μ§€μ—­ λ³€μˆ˜(static local variable)

 

- ν•΄λ‹Ή λ³€μˆ˜μ˜ 곡간이 ν•¨μˆ˜μ˜ μˆ˜ν–‰μ΄ μ’…λ£Œλœ 후에도 μœ νš¨ν•˜λ‹€

- κ·ΈλŸ¬λ―€λ‘œ ν•¨μˆ˜κ°€ μ’…λ£Œλœ 후에도 κ·Έ 값이 μœ μ§€λ˜λŠ” νš¨κ³Όκ°€ μžˆλ‹€ (μ „μ—­λ³€μˆ˜μ²˜λŸΌ νš¨κ³Όκ°€ λ‚˜νƒ€λ‚¨)