C++:typedef
Typedef 함수포인터
일반적인 함수포인터는 아래와 같다.
위의 내용을 Typedef할 경우 아래와 같이 하면 된다.
응용을 하자면,
이렇게 할 경우 arpf라는 함수포인터 배열 5개가 생성되지만 가독성이 떨어지므로 아래와 같이 수정할 수 있다.
typedef struct
를 사용하는 이유?
1. typedef
를 사용하지 않을 경우:
#include<stdio.h>
struct Point{
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x);
printf("%d \n", p1.y);
return 0;
}
위의 경우, Point
구조체를 사용하고 싶을 경우 struct Point
라고 써야 한다.
2. typedef
를 사용할 경우:
#include<stdio.h>
struct Point{
int x;
int y;
};
typedef struct Point Point;
int main() {
Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x);
printf("%d \n", p1.y);
return 0;
}
위의 경우, Point
구조체를 struct
없이 바로 사용할 수 있다.
3. typedef struct
를 한번에 사용할 수 있다:
#include<stdio.h>
typedef struct Point{
int x;
int y;
} Point;
int main() {
Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x);
printf("%d \n", p1.y);
return 0;
}