我试图通过创建一个泛型结构在 C 中手动实现多态行为,然后派生的结构(如果你愿意的话)可以通过枚举的值来区分,这样我就可以有一个指向泛型类型的指针,将其取消引用为泛型类型,找出它是什么类型,然后将其取消引用为更具体的类型。
typedef struct{
enum type structType;
//... other stuff all the structs share
}generic;
typedef struct{
generic; //does not work, obviously, nor does (generic){};
//... other stuff unique to struct type A
}typeA;
我知道我可以在派生结构中声明一个通用结构的命名实例,但这似乎有点混乱,如果有一个整洁的方法,我不希望这样做。