我需要发送一些有关 VxWorks 消息队列的信息。要发送的信息是在运行时决定的,并且可以是不同的数据类型。我正在为此使用一个结构 -
struct structData
{
char m_chType; // variable to indicate the data type - long, float or string
long m_lData; // variable to hold long value
float m_fData; // variable to hold float value
string m_strData; // variable to hold string value
};
我当前正在通过消息队列发送 structData 数组。
structData arrStruct[MAX_SIZE];
这里的问题是,结构中一次只有一个变量有用,其他两个没有用。因此,消息队列不必要地超载。
我无法使用联合,因为数据类型和值是必需的。
我尝试使用模板,但它不能解决问题。我一次只能发送一种数据类型的结构数组。
template <typename T>
struct structData
{
char m_chType;
T m_Data;
}
structData<int> arrStruct[MAX_SIZE];
是否有保存此类信息的标准方法?
我不明白为什么你不能使用工会。这是标准方式:
struct structData
{
char m_chType; // variable to indicate the data type - long, float or string
union
{
long m_lData; // variable to hold long value
float m_fData; // variable to hold float value
char *m_strData; // variable to hold string value
}
};
通常,您会打开数据类型,然后访问对该类型有效的字段。
请注意,您不能将string
加入工会,因为string
type 是非 POD 类型。我已将其更改为使用指针,该指针可以是 C 以零结尾的字符串。然后,您必须考虑根据需要分配和删除字符串数据的可能性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)