【发布时间】:2014-02-13 04:50:26
【问题描述】:
我有 2 个标头需要定义 2 个类似的结构。一种结构应该与 32 字节对齐,另一种类似但不需要与 32 字节对齐,因此我可以在这里节省一些内存。一个包含在另一个中。这是main_header.h
struct mainStruct
{
int a;
int b;
int c;
char pad[20];
};
下面是sub_header.h
#include "main_header.h"
struct subStruct
{
int a;
int b;
int c;
};
现在我想做的是定义subStruct,只要我更改mainStruct而不是pad[20],所有其他字段都应该在subStruct中更新。也就是说,如果明天,我把mainStruct 变成这样:
struct mainStruct
{
int a;
int b;
int c;
int d;
char pad[16];
};
然后,它应该自动反映在sub_header.h 中
struct subStruct
{
int a;
int b;
int c;
int d;
};
有没有使用某种宏或其他预处理器指令的有效方法?
并且 subStruct 应该始终从 mainStruct 派生而不是其他方式,这非常重要。
提前致谢。
【问题讨论】:
-
你不能将 mainStruct 定义为 subStruct 的成员,例如 struct subStruct{ struct mainStruct * mMainObj;}; ?
-
这里是否有可能使用
C++,因为C++正是您所需要的Inheritance。
标签: c structure redefinition