我在Visual Studio 2010中移植一些舊的代碼,從C到C++,我碰到這個傳來:如何靜態初始化一個包含union的結構數組?
typedef struct OptionDef {
const char *name;
int flags;
union {
void *dst_ptr;
int (*func_arg)(void *, const char *, const char *);
size_t off;
} u;
const char *help;
const char *argname;
} OptionDef;
static const OptionDef options[] = {
{ "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
...
現在失敗,出現語法錯誤。我已經看到Statically initialize anonymous union in C++的響應,但重載構造函數將不起作用,因爲我正在設置一個數組。有沒有其他的方式來做到這一點(而不是隻是重寫代碼不使用聯合)?
更新: 我本來應該更具體的 - 數組包含使用工會各地不同initialisers:
static int is_full_screen;
{ "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
所以只是改變了工會的順序也無濟於事。
不幸的是,得心應手C99語法*指定初始化*沒使它成爲C + + 11 :( – dasblinkenlight