함수 포인터 그룹의 예
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
enum response_type
{
DUMP,
SECOND_CHANCE,
MARRIAGE
};
typedef struct
{
char *name;
enum response_type type;
}response;
void dump(response r)
{
printf("Dear %s,
", r.name);
puts("dump function
");
}
void second_chance(response r)
{
printf("Dear %s,
", r.name);
puts("second_chance function
");
}
void marriage(response r)
{
printf("Dear %s,
", r.name);
puts("marriage function
");
}
int main()
{
response r[] = {
{"Mike", DUMP}, {"Luis", SECOND_CHANCE},
{"Matt", SECOND_CHANCE}, {"William", MARRIAGE}
};
int index;
for (index = 0; index < 4; index++)
{
switch (r[index].type)
{
case DUMP:
dump(r[index]);
break;
case SECOND_CHANCE:
second_chance(r[index]);
break;
case MARRIAGE:
marriage(r[index]);
break;
default:
break;
}
}
return 0;
}
이 프로그램은 실행할 수 있을 뿐만 아니라 정확하다.그러나 코드에는 대량의 함수 호출이 가득 차서 매번 r의 type에 따라 함수를 호출해야 한다. 이렇게 보면 다음과 같다.
switch (r.type)
{
case DUMP:
dump(r[index]);
break;
case SECOND_CHANCE:
second_chance(r[index]);
break;
case MARRIAGE:
marriage(r[index]);
break;
default:
break;
}
이렇게 되면 r의 네 번째 유형을 추가하면 프로그램의 모든 이런 부분을 수정해야 한다.곧 많은 코드가 유지보수를 필요로 할 뿐만 아니라, 이렇게 하면 오류가 발생하기 쉽다.
다음은 함수 포인터 배열을 만들어 위의 코드를 대체하는 방법입니다.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
enum response_type
{
DUMP,
SECOND_CHANCE,
MARRIAGE
};
typedef struct
{
char *name;
enum response_type type;
}response;
void dump(response r)
{
printf("Dear %s,
", r.name);
puts("dump function
");
}
void second_chance(response r)
{
printf("Dear %s,
", r.name);
puts("second_chance function
");
}
void marriage(response r)
{
printf("Dear %s,
", r.name);
puts("marriage function
");
}
void (*replise[])(response) = { dump, second_chance, marriage };
int main()
{
response r[] = {
{"Mike", DUMP}, {"Luis", SECOND_CHANCE},
{"Matt", SECOND_CHANCE}, {"William", MARRIAGE}
};
int index;
for (index = 0; index < 4; index++)
{
replise[r[index].type](r[index]);
}
return 0;
}
보시다시피, 여기는 이미 그렇게 큰 switch 코드가 없습니다.그러나 프로그램은 여전히 위의 초기 프로그램과 같은 실행 결과이다.
이렇게 한 줄의 코드가 위의 큰 switch 코드를 대체합니다!
그렇다면 r의 네 번째 유형을 늘릴 때의 대가를 살펴보자.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
enum response_type
{
DUMP,
SECOND_CHANCE,
MARRIAGE,
DEAD
};
typedef struct
{
char *name;
enum response_type type;
}response;
void dump(response r)
{
printf("Dear %s,
", r.name);
puts("dump function
");
}
void second_chance(response r)
{
printf("Dear %s,
", r.name);
puts("second_chance function
");
}
void marriage(response r)
{
printf("Dear %s,
", r.name);
puts("marriage function
");
}
void dead(response r)
{
printf("Dear %s,
", r.name);
puts("dead function
");
}
void (*replise[])(response) = { dump, second_chance, marriage, dead };
int main()
{
response r[] = {
{"Mike", DUMP}, {"Luis", SECOND_CHANCE},
{"Matt", SECOND_CHANCE}, {"William", MARRIAGE},
{"Zeng", DEAD},
};
int index;
for (index = 0; index < 5; index++)
{
replise[r[index].type](r[index]);
}
return 0;
}
비교해 보면 세 군데만 수정하면 대응하는 함수를 추가할 수 있다!유지 보수 대가가 크게 줄었다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.