선언(declaration)하는 것과 정의(definition)하는 건 뭐가 다른 건가요?
조회수 5639회
1 답변
-
선언은
identifier
와identifier
의type
을 정하는 역할을 합니다.identifier
는int
,char
같은 일반적인type
일지,object
일지, 함수가 될지는 것은 선언부에서 결정 됩니다. 선언은 컴파일러가identifier
를 reference로 받아들일 수 있게 합니다.다음 코드는 전부 선언에 해당하지요
extern int bar; extern int g(int, int); double f(int, double); class foo;
정의는
identifier
를 구현(implement)하는 역할을 합니다. 링커가reference
와entity
를 연결하는데 쓰이지요. *정의는 선언과 함께 쓰일 수 있습니다.다음 코드는 위의 선언과 대응하는 정의입니다
int bar; int g(int lhs, int rhs) {return lhs*rhs;} double f(int i, double d) {return i+d;} class foo {};
identifier
를 선언하는 데에는 거의 제약이 없습니다. 다음의 선언은C
/C++
에서 모두 문제 없이 쓸 수 있습니다. (다만 같은 scope내에서는 이 중 하나만 쓸 수 있습니다.)double f(int, double); double f(int, double); extern double f(int, double); // the same as the two above extern double f(int, double);
symbol
을 선언해놓고 정의하지 않았을 때에는 linker가 해당symbol
을 알려주고symbol
을 여러 번 정의해 놓은 경우 링커가 어떤 정의를 link 해야 할지 알 수 없기 때문에 중복된symbol
을 알려줍니다.
댓글 입력