정적변수는 지역변수와 전역변수를 섞어서 보완한 것이다.
지역변수는 변수가 휘발성이 강하다는 단점이 있고
전역변수는 모~든 공간에서 사용이 가능하다는 단점이 있다.
두 변수의 단점을 보완한 변수가 정적변수이다
정적변수는 해당지역에서 사용되며, 데이터가 프로그램이 끝날때까지 남아있다.(지역이 끝나도 소멸되지 않는다)
#include <iostream>
using namespace std;
void Static_Counter()
{
static int cnt;
cnt++;
cout << "Current Static cnt: " << cnt << endl;
}
void Counter()
{
int cnt = 0;
cnt++;
cout << "Current cnt: " << cnt << endl;
}
int main()
{
for (int i = 0;i < 10;i++) {
Static_Counter();
Counter();
}
return 0;
}

Static_Counter()에서 선언된 static int cnt;Counter() 에서 선언된 int cnt = 0;#include <iostream>
using namespace std;
class SoSimple
{
private:
static int simObjCnt;
public:
SoSimple()
{
simObjCnt++;
cout << simObjCnt << "번째 SoSimple 객체" << endl;
}
};
// static 멤버변수는 객체 소속(멤버)이/가 아니라 class 소속이기 때문에 class 밖에서 초기화가 이뤄져야한다.
int SoSimple::simObjCnt = 0; // 클래스 정적변수 초기화 방법
class SoComplex
{
private:
static int cmxObjCnt;
public:
SoComplex()
{
cmxObjCnt++;
cout << cmxObjCnt << "번째 SoComplex 객체" << endl;
}
SoComplex(SoComplex ©)
{
cmxObjCnt++;
cout << cmxObjCnt << "번째 SoComplex 객체" << endl;
}
};
int SoComplex::cmxObjCnt = 0;
int main()
{
SoSimple sim1;
SoSimple sim2;
SoComplex com1;
SoComplex com2 = com1;
SoComplex();
return 0;
}

int SoSimple::simObjCnt = 0;처럼 멤버변수에 static으로 선언된 변수
- static 멤버변수는 객체 소속이 아니라 클래스 소속이기 때문에 클래스의 밖에서 초기화 해줘야 함
또한 static멤버변수는 객체 소속이 아니라 클래스 소속이기 때문에 클래스를 통한 접근도 가능하다
#include <iostream>
using namespace std;
class SoSimple
{
public:
static int simObjCnt; // 외부접근 가능
public:
SoSimple()
{
simObjCnt++;
}
};
int SoSimple::simObjCnt = 0;
int main()
{
cout << SoSimple::simObjCnt << "번째 SoSimple 객체" << endl; // 클래스 멤버이기 때문에 :: 사용해서도 접근이 가능하다
SoSimple sim1;
cout << sim1.simObjCnt << "번째 SoSimple 객체" << endl;
SoSimple sim2;
cout << SoSimple::simObjCnt << "번째 SoSimple 객체" << endl;
cout << sim2.simObjCnt << "번째 SoSimple 객체" << endl;
return 0;
}

cout << SoSimple::simObjCnt << "번째 SoSimple 객체" << endl;에서SoSimple::simObjCnt를 통해 static 멤버변수에 접근한 형태
static 멤버함수는 static 멤버변수와 특성이 동일함
class SoSimple
{
private:
int num1;
static int num2;
public:
SoSimple(int n) : num1(n)
{ }
static void Adder(int n)
{
num1 += n; // 에러발생
num2 += n;
}
};
int SoSimple::num2=0;
num1 += n;에서 에러가 발생하는데 이는 static으로 선언되지 않은 멤버변수에 접근해서 발생한 에러이다.
static void Adder(int n)구문이 static으로 객체소속이 아니라 클래스 소속이다.
num1은 객체 소속
mutable 키워드는 const 함수 내에서의 값의 변경의 예외적으로 허용한다.
사용하는 것 지양
#include <iostream>
using namespace std;
class SoSimple
{
private:
int num1;
mutable int num2;
public:
SoSimple(int n1,int n2) : num1(n1),num2(n2)
{ }
void ShowSimpleData() const
{
cout << num1 << ", " << num2 << endl;
}
void CopyToNum2() const
{
num2 = num1;
}
};
int main()
{
SoSimple sm(1, 2);
sm.ShowSimpleData();
sm.CopyToNum2();
sm.ShowSimpleData();
return 0;
}
