0

I need an array of structs that is going to be used to draw the elements of the screen based on what is the selected point (which is just a number).

Mainly I'm interested in the correct syntax of declaring a struct that has function pointers as members, creating an array that has elements of that struct type and passing arguments to those functions. Something like the code below but with correct syntax.

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 
}
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
vangde
  • 7
  • 3
  • 3
    why does `menuStruct` have function pointers as members instead declaring methods that are overriden by subclasses? – 463035818_is_not_an_ai Sep 26 '18 at 10:19
  • 1
    Possible duplicate of [How do function pointers in C work?](https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work) – apple apple Sep 26 '18 at 10:21
  • 1
    plus you have `std::function` in `c++` – apple apple Sep 26 '18 at 10:21
  • Why are you using a C-ism erzatz of OOP in C++? – YSC Sep 26 '18 at 10:40
  • 2
    @user463035818 There are two clear benefits to not doing the inheritance dance: you can define a new menu without the hassle of defining Yet Another Subclass with overrides that mostly do nothing except forward to another function, and the whole system is defined in one single place instead of spread out over all those subclasses. – molbdnilo Sep 26 '18 at 10:53
  • @molbdnilo sure, i am not a big fan of using inheritance for everything either, was just trying to find out what was OPs motivation to enter the hassle of using function pointers when inheritance would do as well – 463035818_is_not_an_ai Sep 26 '18 at 11:15

1 Answers1

4

Syntax would be

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
}

and possible usage:

allMenues[1].rightEdgeSymbol(false);
Jarod42
  • 203,559
  • 14
  • 181
  • 302