有:
struct packet_sample_t {
int common_id;
int some;
char data[];
float foo;
}
具有以下功能:
void dispatch_packet(void *packet);
æˆ‘çš„ç›®æ ‡æ˜¯è§£æžæ•°æ®åŒ…ID,然åŽè°ƒç”¨å…¶å¤„ç†ç¨‹åºï¼Œä½†æˆ‘æ— æ³•ä»Žcommon_id
检索结构å˜é‡void *
。
我想在高级è¯è¨€ä¸åˆ›å»ºç±»ä¼¼interface
的内容,å‡è®¾æˆ‘的所有数æ®åŒ…结构都应该包å«å˜é‡common_id
。
所以我æ£åœ¨å¯»æ‰¾ä¸€äº›åƒä¸‹é¢é‚£æ ·å·¥ä½œçš„东西:
struct packet_base {
int common_id;
}
void dispatch_packet(void *packet) {
int common_id = ( (packet_base *)packet )->common_id;
switch(common_id) {...}
}
void test() {
packet_sample_t packet = {.common_id = 10, ...};
dispatch_packet((void *) &packet); //this function should retrieve `common_id`
packet_other_t other = {.common_id = 1};
dispatch_packet((void *) &other); // again with another packet
}
我对Cè¯è¨€å¹¶ä¸ç†Ÿæ‚‰ï¼Œæˆ‘真的ä¸çŸ¥é“如何åšåˆ°è¿™ä¸€ç‚¹ã€‚但简å•æ¥è¯´ï¼Œæˆ‘希望能够将数æ®åŒ…转å‘到其packet_base,它们共享一个公共å˜é‡ã€‚
编辑:示例ä¸çš„更多细节
ç”案 0 :(得分:1)
您的技术有效。有a number of ways to do struct inheritance in C,这就是其ä¸ä¹‹ä¸€ã€‚ 21st Century Cå¯èƒ½å¯¹æ‚¨å’ŒObject-Oriented Programming with ANSI C都很好。
您对如何声明和使用结构和类型有疑问。让我们æ¥çœ‹çœ‹ã€‚
struct packet_base {
int common_id;
};
è¿™ç§ç±»åž‹ä¸ºstruct packet_base
。如果è¦å£°æ˜ŽæŒ‡å‘æ¤ç±»åž‹çš„指针,则需è¦ç¼–写struct packet_base *packet
。如果您想è¦æŠ•å°„æ¤ç±»åž‹çš„å˜é‡ï¼Œåˆ™(struct packet_base *)thing
。
è¿™å¾ˆçƒ¦äººï¼Œæ‰€ä»¥ä½ é€šå¸¸ä½¿ç”¨typedef
为结构声明一个类型别å。è¯æ³•ä¸ºtypedef <type> <alias>
typedef struct {
int common_id;
} packet_base_t;
表示类型struct { int common_id; }
的别å为packet_base_t
。现在您å¯ä»¥ä½¿ç”¨packet_base_t
作为类型。 packet_base_t *packet
声明一个指针和(packet_base_t *)thing
强制转æ¢ã€‚
通过修å¤ï¼ŒåŠ 上一些å°é”™è¯¯ï¼Œå®ƒå¯ä»¥æ£å¸¸å·¥ä½œã€‚有关char *data
vs char data[]
çš„ä¿¡æ¯ï¼Œè¯·å‚阅What is the difference between char array vs char pointer in C?。
typedef struct {
int common_id;
int some;
char *data;
float foo;
} packet_sample_t;
typedef struct {
int common_id;
} packet_base_t;
void dispatch_packet(void *arg) {
// It's simpler to cast to a new variable once then to muck
// up the code with casts.
packet_base_t *packet = (packet_base_t *)arg;
int common_id = packet->common_id;
printf("id: %d\n", common_id);
}
ç”案 1 :(得分:0)
您å¯ä»¥ä½¿ç”¨union
在å•ä¸ªç»“æž„ä¸èšåˆä¸åŒç±»åž‹çš„æ•°æ®
struct packet1_t
{
// Packet1 specific data
......
}
struct packet2_t
{
// Packet2 specific data
......
}
struct packet_t
{
int common_id;
union
{
struct packet1_t packet1;
struct packet2_t packet2;
......
}
} packet;
çŽ°åœ¨ï¼Œæ ¹æ®ID,您å¯ä»¥ä»Žè”åˆä¸é€‰æ‹©æ£ç¡®çš„类型:
int do_something_with_packet(packet_t packet)
{
switch (packet.common_id)
{
case 1:
do_something_with_packet1(packet.packet1);
break;
case 2:
do_something_with_packet2(packet.packet2);
break;
..................
}
....
}