const / static / extern
1) const

2) 클래스 멤버로서의 const

*더 알아보기
생성자에서 const 멤버 변수를 초기화 해줄 때 사용하는 이 방법은 C 에서 기본적으로 변수를 초기화하는 방법입니다. 하지만 이 방법은 읽기에 난해하여 근래에는 사용되지 않는 방법입니다. C++가 이런저런 개념들에 C 의 문법을 모두 사용하기 때문에 벌어지는 웃기는 상황인 것입니다.
예제코드)
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#define MYVALUE 100
class Person {
public:
const int age;
Person(int n) : age(n) {
}
};
int main() {
const char* str;
char* myStr = new char[100];
strcpy(myStr, "Hello World");
str = myStr;
printf("%s", str);
return 0;
/*int input;
fseek(stdin, 0, SEEK_END);
scanf("%d", &input);
Person* p = new Person(input);
printf("%d", p->age);
return 0;
*/
}
3) static 키워드

dynamic 함수의 특징 : 자기 자신의 인스턴스가 있다.
static 함수의 특징 : this 라는 키워드가 존재 할 수 없다. 단 static int b; 같은 스태틱 변수는 사용이 가능하다.
static 변수 : 스태틱 변수는 항상 메모리에 존재해야하는 변수이기 때문에 cpp파일에서 몸체를 만들어줘야한다.
이를 예제코드를 통해 알아봅니다.
3-1) static 멤버 변수의 사용
이를 소스코드를 이용해 알아봅니다
예제코드)
헤더파일 )
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
class Myclass {
public:
static int static_member_int;
int a;
void foo(); // dynamic
static void goo(); // static
};
cpp파일)
#include "Header.h"
int main() {
Myclass::static_member_int = 100;
printf("%d", Myclass::static_member_int);
return 0;
}
int Myclass::static_member_int = 100;
void Myclass::foo() {
}
void Myclass::goo() {
}
4) extern 키워드 = 프로그램 전체에서의 static

*더 알아보기
실제로 extern 키워드를 많이 활용하면서 개발하는 것은 추천하는 개발 방법이 아닙니다. 실제로 테트리스 프로그램도 extern 이나 static 키워드를 사용하지 않고도 얼마든지 만들 수 있습니다.
다만 이를 이용해 extern 을 활용하는 방법을 배워보기 위해 일부러 사용한 것입니다.
예제코드 :
테트리스 만들기에서 헤드파일(Header.h)
extern int displayData[GRID_HEIGHT][GRID_WIDTH]; // 선언부분
Source.cpp 파일
int displayData[GRID_HEIGHT][GRID_WIDTH] = {0,}; // 정의 부분