gpt4 book ai didi

c++ - 包含函数作为成员的结构数组 C++

转载 作者:行者123 更新时间:2023-11-27 22:37:25 25 4
gpt4 key购买 nike

我需要一个结构数组,用于根据所选点(只是一个数字)绘制屏幕元素。

我主要感兴趣的是正确的语法,即声明一个具有函数指针作为成员的结构,创建一个具有该结构类型元素的数组,并将参数传递给这些函数。类似于下面的代码,但语法正确。

struct menuStruct 
{
void *leftEdgeSymbol(boolean show);
void *certerScreenSymbol(boolean show);
void *rightEdgeSymbol(boolean show);
void *buttonFunction(void);
};


menuStruct allMenues[] =
{
{drawLeftArrow(), drawTempSettingText(), drawRightArrow(), mainMenu1()}, //0
{eraseLeftArrow(), drawHumidTargetText(), drawRightArrow(), mainMenu2()}, //1
{drawLeftMinusSign(), drawSetTempTarget(), drawRightPlusSign(), subMenu101() //2
}




void drawLeftArrow(boolean show)
{
//Draw the arrow with white colour to show or with black to erase
}

void rightArrow(boolean show)
{
//Draw the arrow with white colour to show or with black to erase
}

最佳答案

语法是

struct menuStruct 
{
void (*leftEdgeSymbol)(bool);
void (*certerScreenSymbol)(bool);
void (*rightEdgeSymbol)(bool);
void (*buttonFunction)();
};

void drawLeftArrow(bool show)
{
//Draw the arrow with white colour to show or with black to erase
}

void rightArrow(bool show);

// ...

menuStruct allMenues[] =
{
{&drawLeftArrow, &drawTempSettingText, &drawRightArrow, &mainMenu1}, //0
{&eraseLeftArrow, &drawHumidTargetText, &drawRightArrow, &mainMenu2}, //1
{&drawLeftMinusSign, &drawSetTempTarget, &drawRightPlusSign, &subMenu101} //2
}

和可能的用法:

allMenues[1].rightEdgeSymbol(false);

关于c++ - 包含函数作为成员的结构数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52515358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com