#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int state;
}JPG;
typedef struct{
int state;
int data;
}PNG;
typedef struct{
int state;
}AVI;
JPG jpg;PNG png; AVI avi;
void vist_0(int s){
printf("Frist vist! state:%d\n",jpg.state);
}
void vist_1(int s){
printf("Sencond vist!state:%d\n",png.state);
}
void vist_2(int s){
printf("Third vist!state:%d\n",avi.state);
}
void vistor(int v,int s){
switch(v){
case 0:vist_0(0);break;
case 1:vist_1(1);break;
case 2:vist_2(2);break;
}
}
int main(){
vistor(0,1);
vistor(1,1);
vistor(2,1);
return 0;
}
typedef struct {
void (*accept)(void (*visit)(void));
} Element;
void accept(Element* self, void (*visit)(void)) {
visit();
}
void visitA() { printf("Visited A\n"); }
Element elementA = { .accept = accept };
// 使用
elementA.accept(&elementA, visitA); // 输出 "Visited A"