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.