κΈ€ μž‘μ„±μž: λ˜₯폴베.
λ°˜μ‘ν˜•

1. μ„€λͺ…


 static ν‚€μ›Œλ“œλŠ” C++μ—μ„œ νŠΉλ³„ν•œ νŠΉμ„±μ„ λΆ€μ—¬ν•˜λŠ” 데 μ‚¬μš©ν•œλ‹€.

static으둜 μ„ μ–Έλœ 것듀은 ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ μ‹œκ°„ λ™μ•ˆ ν•œ 번만 정적 데이터 μ˜μ—­μ— μ €μž₯되고, ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ λ™μ•ˆ μœ μ§€λœλ‹€. 

 

 static ν‚€μ›Œλ“œλŠ” λ‹€μŒκ³Ό 같이 μ‚¬μš©λœλ‹€.

1. ν•¨μˆ˜ λ‚΄μ—μ„œ static λ³€μˆ˜

2. static 클래슀 객체

3. 클래슀 λ‚΄ static 멀버 λ³€μˆ˜

4. 클래슀 λ‚΄ static ν•¨μˆ˜

 

 

2. μ˜ˆμ‹œ


2.1) ν•¨μˆ˜ λ‚΄μ—μ„œ static λ³€μˆ˜

#include <iostream>

using namespace std;

void counter()
{
	static int count = 0;
	cout << count++ << " ";
}

int main()
{
	for (int i = 0; i < 5; i++)
	{
		counter();
	}

	return 0;
}
0 1 2 3 4

ν•¨μˆ˜ λ‚΄μ—μ„œ μ‚¬μš©ν•œ static λ³€μˆ˜λŠ” μ§€μ—­λ³€μˆ˜μ™€ 달리 stack에 μ €μž₯λ˜λŠ” 게 μ•„λ‹ˆλΌ 정적 데이터 μ˜μ—­μ— μ €μž₯λ˜λ―€λ‘œ ν•¨μˆ˜κ°€ λλ‚˜λ„ κ·Έ 값이 μœ μ§€λœλ‹€.

 

2.2) static 클래슀 객체

#include <iostream>

using namespace std;

class Abc
{
    int i;
public:
    Abc()
    {
        i = 0;
        cout << "constructor" << endl;
    }
    ~Abc()
    {
        cout << "destructor" << endl;
    }
};

void f()
{
    static Abc obj;
}

int main()
{
    int x = 0;
    if (x == 0)
    {
        f();
    }
    cout << "END" << endl;
    
    return 0;
}
constructor
END
destructor

static ν‚€μ›Œλ“œλŠ” 클래슀 κ°μ²΄μ—μ„œλ„ λ™μΌν•˜κ²Œ μ μš©λœλ‹€.

객체가 정적 데이터 μ˜μ—­μ— μ €μž₯λ˜λ―€λ‘œ ν”„λ‘œκ·Έλž¨μ΄ 끝날 λ•ŒκΉŒμ§€ μœ μ§€λœλ‹€.

일반적인 κ°μ²΄μ˜€λ‹€λ©΄ f() ν•¨μˆ˜ 호좜이 λλ‚˜λ©΄ νŒŒκ΄΄μžκ°€ ν˜ΈμΆœλμ„ν…λ°, static κ°μ²΄μ΄λ―€λ‘œ ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ λ™μ•ˆ 객체가 μœ μ§€λ˜κ³  ν”„λ‘œκ·Έλž¨μ΄ λλ‚˜μ•Ό νŒŒκ΄΄λœλ‹€.

 

 

2.3) 클래슀 λ‚΄ static ν•¨μˆ˜

#include <iostream>

using namespace std;

class X
{
public:
    static int i;
    X()
    {
        // construtor
    };
};

int X::i = 1;

int main()
{
    X obj;
    cout << obj.i;   // prints value of i
}
1

클래슀 λ‚΄λΆ€μ˜ static 멀버 λ³€μˆ˜λŠ” λͺ¨λ“  κ°μ²΄μ—μ„œ κ³΅μœ ν•œλ‹€.

λ˜ν•œ μƒμ„±μžμ—μ„œ μ΄ˆκΈ°ν™”ν•˜μ§€ μ•ŠμœΌλ©°, 객체 μ΄ˆκΈ°ν™”μ— μ’…μ†λ˜μ§€ μ•Šμ•„ λͺ…μ‹œμ μœΌλ‘œ 클래슀 μ™ΈλΆ€μ—μ„œ 값을 ν• λ‹Ήν•œλ‹€.

μ΄ˆκΈ°ν™”λ˜μ§€ μ•ŠμœΌλ©΄ λ§μ»€μ—μ„œ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

static 멀버 λ³€μˆ˜λŠ” ν•œ 번만 μ •μ˜ν•  수 있고, λ‹€λ₯Έ κ°’μœΌλ‘œλŠ” μž¬ν• λ‹Ή λΆˆκ°€λŠ₯ν•˜λ‹€.

 

 

2.4) 클래슀 λ‚΄ static ν•¨μˆ˜

#include <iostream>

using namespace std;

class X
{
public:
    static void f()
    {
        cout << "f function call" << endl;
    }
};

int main()
{
    X::f();   // calling member function directly with class name
}
f function call

클래슀 λ‚΄ static ν•¨μˆ˜λŠ” 각각의 객체가 μ•„λ‹Œ λͺ¨λ“  ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•œλ‹€.

일반적으둜 직접 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ €λ©΄ .μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ 클래슀 λ‚΄ static ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ €λ©΄ ::μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

이런 static ν•¨μˆ˜λŠ” λ‹€λ₯Έ ν•¨μˆ˜μ— μ ‘κ·Όν•  수 μ—†μœΌλ©° static으둜 μ„ μ–Έλœ ν•¨μˆ˜μ™€ λ³€μˆ˜μ—λ§Œ μ ‘κ·Ό ν•  수 μžˆλ‹€.

 

 

 

3. 참고 및 좜처


https://www.studytonight.com/cpp/static-keyword.php

https://codingcoding.tistory.com/736
λ°˜μ‘ν˜•

'Language > C++' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[C++] keyword: using  (0) 2020.07.23
[C++] keyword: friend  (0) 2020.07.23
[C++] keyword: explicit  (0) 2020.07.22
[C++] keyword: inline, __inline, __forceinline  (0) 2020.07.22
[C++] keyword: const  (0) 2020.07.22