cpp_static - 8BitsCoding/RobotMentor GitHub Wiki
static
- ๋ฒ์์ ์ ํ์ ๋ฐ๋ ์ ์ญ ๋ณ์(ํต์ฌ์ ์ ์ญ๋ณ์๋ก์ ์ง์์ง์ง ์๋ ๋ค๋์ ~)
- ๋ฒ์?
- ํ์ผ ์
- ๋ค์์คํ์ด์ค ์
- ํด๋์ค ์
- ํจ์ ์
extern์ ๋ญ๋ฐ?
- ๋ค๋ฅธ ํ์ผ์ ์ ์ญ๋ณ์์ ์ ๊ทผ์ ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
- extern์ผ๋ก ๋์ด ์๋ค๋ฉด? ์ปดํ์ผ๋ฌ๋ ์ด๋ ๊ฒ ์๊ฐํ๋ค. ๋ด๊ฐ ๊ฐ์ gloval value๋ ์๋์ง๋ง ์ด๋๊ฐ ์ ์ธ์ด ๋์ด ์๋ค. ์ฐพ์๋ณด์.
// ExternTest.h
extern int globalValue; // ์ด๋์๊ฐ ์ ์ธ์ด ๋์ด ์๋ค๊ณ ์๋ฆฐ๋ค.
void IncreaseValue();
// ExternTest.cpp
int globalValue = 2;
void IncreaseValue() {
globalValue++;
}
static์ extern์ ๋ง๋๋ฐ ์ฌ์ฉ๋๊ธฐ๋ ํ๋ค.
// ExternTest.h
extern int globalValue;
void IncreaseValue();
// ExternTest.cpp
static int globalValue = 2; // ์ธ๋ถ์์ ํธ์ถ์ ๋งํฌ์๋ฌ๊ฐ ๋ฐ์, static์ ๋ฒ์๋ฅผ ์ ํํ ์ ์ญ ๋ณ์์ด๊ธฐ ๋๋ฌธ์ด๋ค.
void IncreaseValue() {
globalValue++;
}
static์ ์ฌ์ฉ 1 - ํจ์ ์ Static
void Accumulate(int number)
{
static int result = 0;
result += number; // ํจ์์ ๋ค์ด์ฌ๋๋ง๋ค ๋์ ๋๋ค.
std::cout << "result = " << result << std::endl;
}
// main
result = 1; // ์ ๊ทผ ์์ฒด๊ฐ ๋ถ๊ฐ๋ฅ
static์ ์ฌ์ฉ 2 - ์ ์ ๋ฉค๋ฒ ๋ณ์
class Cat {
public:
Cat();
private:
static int mCount;
};
// Cat.cpp
int Cat::mCount = 0; // ์ ๊ทผ ๊ฐ๋ฅ
-
ํจ์์์์ ์ ์ ๋ณ์๋ฅผ ๋ฃ์ง๋ง ๊ฒ
ํด๋์ค ์์์ ๋ฃ์.
-
์ ์ญ๋ณ์ ๋์ ์ ์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์ฐ์.
-
C์คํ์ผ์ ์ ์ ๋ณ์๋ฅผ ์ธ ์ด์ ๊ฐ ์์.
์ง๋ฌธ
์๋์ฝ๋์ ๋ฉ๋ชจ๋ฆฌ์ ์กด์ฌํ๋ mCount๋ณ์๊ฐ ๋ช๊ฐ ์ผ๊น? mCount์ ๊ฐ์?? new๋ก ๋ฉ๋ชจ๋ฆฌ ํ ๋นํจ์ ๊ธฐ์ตํด๋ณด์
class Cat {
public:
Cat(int age, const string& name);
private:
static int mCount;
};
// Cat.cpp
int Cat::mCount = 0;
Cat::Cat(int age, const string& name) {
++mCount;
}
// Main
Cat* myCat = new Cat(2, "Coco1");
Cat* yoursCat = new Cat(5, "Coco2");
Cat* hisCat = new Cat(7, "Coco3");
Cat* herCat = new Cat(9, "Coco4");
๋ต์ 4๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ 1๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํด์ ์ ์ธํ๋๋ผ๋ ๊ฒฐ๊ตญ์ static์์ ๊ธฐ์ตํ์~
static์ ์ฌ์ฉ 3 - ์ ์ ๋ฉค๋ฒ ํจ์
- ๋ ผ๋ฆฌ์ ๋ฒ์์ ์ ํ ๋ ์ ์ญ ํจ์
- ํด๋น ํด๋์ค์ ์ ์ ๋ฉค๋ฒ์๋ง ์ ๊ทผ ๊ฐ๋ฅ
- ๊ฐ์ฒด๊ฐ ์์ด๋ ์ ์ ํจ์๋ฅผ ํธ์ถ ํ ์ ์๋ค.(์ด๊ฑธ์ํด์ ์ด๋ค.)
Math::Square(10);
class Math {
public:
static int Square(int number);
};
// Math.cpp
int Math::Square(int number) {
return number * number;
}
์ด๋ฐ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ๋๋??
class Cat {
public:
Cat(const std::string& name);
static const char* GetName(); // ์ ์ ๋ฉค๋ฒ ํจ์ ๋ฑ๋ก
private:
char* mName;
static int mCount;
};
// Cat.cpp
const char* Cat::GetName() {
return mName; // ?? mName์ static์ด ์๋๋ฐ? ๋ฆฌํด์ด ๊ฐ๋ฅํ๊ฐ ??
}
์ฐ์ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์! ์ปดํ์ผ๋ฌ์ ๋ฐ๋ผ์ ์ค๋ฅ๋ฉ์์ง๊ฐ ๋ค๋ฅผ ์ ์์ผ๋ ์ฃผ์ํ ๊ฒ!
Example
// Math.h
class Math {
// ...
static int Ceil(float value);
static int Floor(float value);
static float Power(float value);
static int Round(float value);
}
static ์ฌ์ฉ
static ๋ณ์:
- ๊ฐ์ ํด๋์ค ํ์ผ๋ก ์ ์ธ๋ ์ฌ๋ฌ ๊ฐ์ฒด๋ค์ด ๋์ผํ ํ๋์ ์๋ฃ๋ฅผ ๊ณต์ ํ๊ฒ ํด์ค๋ค.
- ์ฌ์ฉ์ ํด๋์ค ์ ์ ์ ๋ฉค๋ฒ๋ฅผ static์ด๋ ํค์๋๋ก ์ ์ํ๋ค.
- ํด๋์ค ๋ด์ ์ ์ธ๋ ์ ์ ๋ฉค๋ฒ๋ ์ ์ ๋ฉค๋ฒ๊ฐ ์ ์ธ๋ ํด๋์ค๋ก ์ฌ์ฉ ์์ญ์ด ์ ํ๋ ์ ์ญ๋ณ์๋ผ ์๊ฐํ์.
- ํด๋์ค ๋ด์์ ์ ์ ๋ฉค๋ฒ๋ฅผ ์ ์ธํ ๋ ์ ์ ๋ฉค๋ฒ ์์ฒด๊ฐ ์ ์๋๋ ๊ฒ์ ์๋๊ธฐ ๋๋ฌธ์ ํด๋์ค ๋ฐ์์ ์ ์ ๋ฉค๋ฒ๋ฅผ ์ ์ํ๋ ์ ์์ ํ์ํ๋ค.
- ๋ชจ๋ ์ ์ ๋ฉค๋ฒ ๋ณ์๋ ํน๋ณํ ์ด๊ธฐ๊ฐ์ ๋ช ์ํ์ง ์๋ ํ 0์ผ๋ก ์ด๊ธฐํ ๋๋ค.
class Person {
char name[20];
int age;
int count; // Person ๊ฐ์ฒด๊ฐ ๋ช๊ฐ๊ฐ ์์ฑ๋๋์ง countํ๊ณ ์ถ์
Person() {
count++; // ๋ชฉ์ ์ ์๊ฒ.
ํด๊ฒฐ๋ฐฉ๋ฒ์...
class Person {
char name[20];
int age;
static int count; // Person ๊ฐ์ฒด๊ฐ ๋ช๊ฐ๊ฐ ์์ฑ๋๋์ง countํ๊ณ ์ถ์
Person() {
count++; // ๋ชฉ์ ์ ์๊ฒ.
static ํจ์: