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

时间:2018-09-26 10:16:58

标签: c++ arrays struct

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

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

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 
}

1 个答案:

答案 0 :(得分:4)

语法应为

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);