C언어에서 함수포인터를 typedef 로 선언하면 어떤 효과가 있나요 ?
글쓴이: dingkyu / 작성시간: 화, 2010/01/19 - 11:44오전
소스코드 분석중 다음과 같은 코드를 발견하였습니다.
typedef void (*FUNCTION)(int number, char *pName);
이렇게 정의해 놓고 다른 함수의 인수에서 위의 FUNCTION을 받는 부분을 보았습니다. 다음과 같습니다.
BOOL someFunction(HINSTANCE hInstance, FUNCTION callback)
{
....
}
궁금한 것은 위처럼 함수포인터를 typedef 로 선언하게 되면 어떤 효과가 있는지 이해를 못하겠습니다. 밑에 함수에 인수로서 다른 함수포인터를 받기위해 그냥 저렇게 하나의 데이터타잎으로 만든것인지,...
이렇게 하는 테크닉에 대해 설명해 주시면 고맙겠습니다.
Forums:
효과라기 보다도..
BOOL someFunction(HINSTANCE hInstance, void (*callback)(int number, char *pName))
{
}
이렇게 쓰는거랑 똑같지만 보기좋고, 짧다는거.
va_arg 등 복잡한
va_arg 등 복잡한 매크로와 연관된 경우라면 typedef를 써야만 할 수도 있습니다.
http://cinsk.org/cfaqs/html/node17.html#15.11
물론 대부분의 경우에는 미관상의 이유나 Win32 API에서 HANDLE, HWND, HFILE 등을 모두 별도로 typedef하는 것과 같은 이유에서 사용합니다.
> What is the difference between software and hard water?
} Bugs drown in hard water, but live forever in software.
Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.
FUNCTION 을 type 처럼 쓸 수 있습니다.
typedef void (*FUNCTION)(int number, char *pName);
vector '<'FUNCTION'>' _functions;
std::vector<void (*)(int,
std::vector<void (*)(int, char *)> _functions;
이 코드도 잘 동작합니다.
> What is the difference between software and hard water?
} Bugs drown in hard water, but live forever in software.
Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.
댓글 달기