선언(declaration) : identifier를 소개하는 것, (identifier에 대한 모든 정보를 제공할 필요는 없다)
- 객체라면 객체의 타입과 이름만
- 함수라면, 파라미터의 개수, 각 파라미터의 타입, 리턴 타입 정도만
정의(definition) : identifier와 연관된 entity, identifier에 대한 모든 정보를 제공하는 것
- type이라면 type과 관련된 모든 상세 정보
- object라면, 공간이 allocated 되도록 해야 하고, object가 created 되도록 하는 것
- function이라면, 함수 본문
객체의 경우에는 객체의 선언이 곧 객체의 정의가 된다.
선언은 여러번 할 수 있지만, 정의는 한 번만 일어난다.
예시
int count; // declare and define count
extern double alpha; // only declare alpha
void func() { // declare and define func
int n; // declare and define n
double x = 1.0; // declare and define x
}
bool isOdd(int); // declare isOdd
bool isOdd(int x); // declare isOdd (x ignored)
bool isOdd(int x){ // declare and define isOdd
return x % 2;
}
struct Thing; // declare Thing
struct Vector2 { // declare and define Vector2
double x;
double y;
};
변수 선언(객체 선언): 객체의 identifer, 객체의 type만 지정해주면 된다.
변수 정의(객체 정의): 변수의 선언 및 객체 생성에 필요한 모든 정보를 제공한다.
int count; // declare and define
double alpha; // declare and define
extern double gamma; // declare only
'C++' 카테고리의 다른 글
[C++] 참조 (1) | 2023.11.22 |
---|---|
[C++] 포인터 (2) | 2023.11.22 |
[C++] g++ 사용법 및 g++ 옵션들 (1) | 2023.11.22 |
Windows에 gcc 설치하기 (0) | 2023.11.08 |
[C++] vector 정리 (0) | 2023.09.16 |