首页 > 解决方案 > C++,创建具有可选值的结构

问题描述

我需要创建具有可选值的结构:

typedef struct pkt_header{
    unsigned short Packet_Type;
    unsigned short Unprotected_Payload_Length;
    unsigned short Protected_Payload_Length; // optional (present/not present)
    unsigned short Version;   
} PKT_HEADER;

当字段不存在时,我如何有时使用pkt_header->Protected_Payload_Length有时不使用此值?struct我的第一个想法是在我不使用该字段并使用for 存储我的值时声明unsigned char * Protected_Payload_Length并传递。NULLunsigned char*unsigned short

typedef struct pkt_header{
    unsigned short Packet_Type;
    unsigned short Unprotected_Payload_Length;
    unsigned char * Protected_Payload_Length; // optional
    unsigned short Version;   
} PKT_HEADER;

我像这样准备我的数据包(并发送这个):

PKT_HEADER header;
header.Packet_Type                = 0x0001;
header.Unprotected_Payload_Length = 0x0b00;
header.Protected_Payload_Length   = NULL; 
header.Version                    = 0x0000;

我收到回复并这样做:

PKT_HEADER * header= (PKT_HEADER*)recvbuf;
printf("Packet_Type                : %04x\n", header->Packet_Type);
printf("Unprotected_Payload_Length : %04x\n", header->Unprotected_Payload_Length);
printf("Version                    : %04x\n", header->Version);

但在这种情况下,如果我理解正确,unsigned char * Protected_Payload_Length包含一个长度为 4 个字节的指针,然后header->Protected_Payload_Length包含4 bytes但我需要0 byte,因为在这种精确情况下不存在值/字段。

我是否必须以数据格式声明适当的结构,还是有其他方法可以使用这些结构?

谢谢你的帮助。

标签: c++struct

解决方案


谨防。结构可以有填充,成员在内存中不一定是相邻的。此外,不允许将某物重新解释为PKT_HEADER当某物不是PKT_HEADER对象时。而不是铸造:

PKT_HEADER * header= (PKT_HEADER*)recvbuf; 

你可能应该使用memcpy. 说了这么多,现在到你的实际问题......

如果您依赖结构中具有特定顺序的成员,则不能选择继承。在内存中,基础对象首先出现,然后是派生成员,您不能混合使用。例如

 struct foo {
     int x;
 };
 struct bar : foo {
     int y;
     int z;
 };

然后一个bar对象将在内存中

 | x | optional padding | y | optional padding | z | optional padding |

没有简单的获取方法| y | x | z |

如果您想要两种不同的类型,最简单的方法是定义两种不同的类型:

struct PKT_HEADER_A {
    unsigned short Packet_Type;
    unsigned short Unprotected_Payload_Length;
    unsigned short Protected_Payload_Length; // present
    unsigned short Version;   
};


struct PKT_HEADER_B {
    unsigned short Packet_Type;
    unsigned short Unprotected_Payload_Length;
    //unsigned short Protected_Payload_Length; // not present
    unsigned short Version;   
};

请注意,您通往typedef结构的方式是 C-ism。在 C++ 中没有必要(也不推荐)。


推荐阅读