Menu Polymorphism

I have a menu class which contains a pointer array to other menu objects, as well as a void pointer to point to a "value".

I'd like to rewrite my class with polymorphism, since a menu object either contains other menus or a value and not both.

At the moment if a menu contains other menu's and not a "value" then the void pointer will point to 0, or vica versa if it contains a value but no other menu's then the menu* pointer will point to 0.

So I thought of making 'menu' my base class and having derived classes menuitem and valueitem. However since both derived classes would still be handled as a 'menu' object I have to use function overloading.

Which brings me to the question, if I have to initialize virtual functions for void value* and menu** inside the base class which will be initialized to 0 regardless whether the derived class uses them or not, is there any point using polymorphism/overloading in this case?

I was hoping to save some memory but since menulist is dynamic does it make any difference either way?

Here is my original menu class definition without polymorphism:

  menu::menu(char *myname="", byte y=0, byte x=0, int c_type=0, byte m_color=0)
  {
    strcpy(name, myname);
    color=m_color;
    type=c_type;
	strcpy(unit,"");
	readonly=true;
    menulist=0;
    previous_menu=0;
    value=0;
    entryfunction=0;
    exitfunction=0;
    maxy=(int)y;

    if (y>0)
    {
      menulist = new menu**[y+1];
      menulist[y]=0;
      for (byte i=0; i<=y; i++)
      { 
        menulist[i] = new menu*[x+1];
        for (byte p=0; p<=x; p++)
          menulist[i][p]=0;
      }
      
    }
  }


  menu::~menu(){
    for(int i = 0; menulist[i]!=0; ++i) {
      delete [] menulist[i];
    }
    delete [] menulist;

  }

So the base class would contain name only, the derived menuitem class would contain everything but 'value' and 'unit', and the derived valueitem would contain only name, value, and unit.

Note: will rewrite the void pointer as a template function at a later date.

I am asking whether or not a base class generally is required, when inheriting classes will have to initialize variables that wont be used, in a virtual function in the base class anyway? (In this case because a 'menu' item can either be another menu or just a value).

Am I missing something? How else can I treat the inheriting classes as the base class with separate variables? (menuitem contains pointer array to other menu objects but valueitem only contains an int or float).

Your question is quite unclear. The code snippet is not very helpful either, lacking member declarations / definitions and prototypes. Perhaps post a pared-down, but complete, example with just the necessary members. It should compile. Then, post example code of what you're trying to do.

The key feature of polymorphism is that using a pointer to the base class you call pure virtual methods that are defined in that base class. The different derived classes implement those methods differently depending on the desired functionality.

So, one pointer type can invoke many different types of behavior. Consider:

class Musician {
  public:
    virtual void play() = 0;
};

class GuitarPlayer : public Musician {
  public:
    virtual void play() {
      // pluckStrings();
    }
};

class TrumpetPlayer : public Musician {
  public:
    virtual void play() {
      // blowIntoHorn();
    }
};

GuitarPlayer guitar;
TrumpetPlayer trumpet;

Musician *musicians[] = {&guitar, &trumpet};
const uint8_t numMusicians = sizeof(musicians) / sizeof(musicians[0]);

void setup() {
}

void loop() {
  for (uint8_t i = 0; i < numMusicians; i++) {
    musicians[i]->play();
  }
}

Unless that's the model you're going for, polymorphism may not be helpful. Regardless, it probably won't save you memory.