0

如:

enum {
    SPICE_MSG_CURSOR_INIT = 101,
    SPICE_MSG_CURSOR_RESET,
    SPICE_MSG_CURSOR_SET,
    SPICE_MSG_CURSOR_MOVE,
    SPICE_MSG_CURSOR_HIDE,
    SPICE_MSG_CURSOR_TRAIL,
    SPICE_MSG_CURSOR_INVAL_ONE,
    SPICE_MSG_CURSOR_INVAL_ALL,

    SPICE_MSG_END_CURSOR
};

static const spice_msg_handler cursor_handlers[] = {
    [ SPICE_MSG_CURSOR_INIT ]              = cursor_handle_init,
    [ SPICE_MSG_CURSOR_RESET ]             = cursor_handle_reset,
    [ SPICE_MSG_CURSOR_SET ]               = cursor_handle_set,
    [ SPICE_MSG_CURSOR_MOVE ]              = cursor_handle_move,
    [ SPICE_MSG_CURSOR_HIDE ]              = cursor_handle_hide,
    [ SPICE_MSG_CURSOR_TRAIL ]             = cursor_handle_trail,
    [ SPICE_MSG_CURSOR_INVAL_ONE ]         = cursor_handle_inval_one,
    [ SPICE_MSG_CURSOR_INVAL_ALL ]         = cursor_handle_inval_all,
};

我什至无法理解这是什么意思。vc++2008不能通过,怎么改?

4

1 回答 1

0

这称为C99 中引入的指定初始值设定项。例如,要初始化一个数组,您可以按任意顺序指定数组索引:

int a[6] = { [4] = 29, [2] = 15 };

相当于

int a[6] = { 0, 0, 15, 0, 29, 0 };

因此,在您的示例中,使用枚举的定义,使用 的元素cursor_handlers初始化,然后是其他几个值。1010

问题是,VC++2008 不支持大部分 C99 特性,包括这个。因此,您必须使用固定顺序(旧的 C89 方式)初始化数组,或者使用 all 初始化数组0并分配适当的元素。

数组太糟糕了const,所以你必须使用 C89 方式:

static const spice_msg_handler cursor_handlers[] = {0, 0, /*...all 101 0s*/, cursor_handle_init, /*...*/}

如果不是const,你可以使用这个:

static spice_msg_handler cursor_handlers[SPICE_MSG_END_CURSOR] = {};
cursor_handlers[SPICE_MSG_CURSOR_INIT] = cursor_handle_init;
cursor_handlers[SPICE_MSG_CURSOR_RESET] = cursor_handle_reset;
//...
于 2013-07-26T18:18:23.597 回答