Multi-layer menu design issues

Hi guys, it's my first post here although I look in regularly for help and fun.

I am currently trying to design a drop down menu system. My plans seem to fall apart as I try to add a second layer to the menu.

I have created a class MenuBase, holding the initial position and a list of menu items. Menu items are divided into Items and Nodes. Items will be clickable, carrying out a function. Nodes will open another menu layer, containing it's own list of menu items(and possibly nodes)
To allow a single list in my MenuBase I have derived Item and Node from a common class, MenuItem. This stops me from accessing the lists inside my Nodes.

I would like to avoid making all Items into Nodes, as they would hold empty lists but I cannot think of an alternative. Hopefully I have made it all clear enough, if not just let me know and I'll try again. Thanks.
If you are designing, then you shouldn't be coding yet, as that is development.

As for your class, I myself am a bit confused as to what you are doing. What you are saying is that you have Item and Node derive from the same class. And your nodes are working? So you want to make all items nodes. If your nodes are working, then just copy what you have from your Node class into your Item class minus the code that creates a new menu.
This is what I got so far:

MenuBase "has a" one to many MenuItem
MenuBase
{
//list of MenuItems
}

class MenuItem
{
//...
}

class Item : public MenuItem
{
void CarryOutFunction();
//...
}

class Node : public MenuItem
{
//...
private:
//list of MenuItems
}

Here is where you lose me, what are you trying to accomplish? Based on what info I got from your read, what am I missing to understand your problem or goal?

This stops me from accessing the lists inside my Nodes.

I would like to avoid making all Items into Nodes, as they would hold empty lists but I cannot think of an alternative. Hopefully I have made it all clear enough, if not just let me know and I'll try again. Thanks.
Yeah, I realise I should design then develop.
Sorry if I'm not very clear at explaining it. clanmjc, you have it right so far. When I have my list of MenuItems inside MenuBase I cannot access the lists contained inside Nodes as they are being treated as MenuItems.

(skipping details such as object creation for convenience, it should still make sense)

class MenuBase
{
vector<MenuItem*> mItems; //contains [Item, Node, Item];
}

class MenuItem{}

class Item : public MenuItem{}

class Node : public MenuItem
{
vector<MenuItem*> nodeItems; // contains [Item]
}

If I try to access MenuBase.mItems[1]->nodeItems[0]
I will run into the issue that MenuItem does not contain the vector nodeItems.

The only solution I can think of is to give MenuItem the vector (nodeItems), but that means the class Item is holding a useless vector. Does this seem like an acceptable trade off, or is there a better way?
This is where you will need to use polymorphism.

Create a virtual method in MenuItem that has a default implementation. It's pretty useless for an Item, but Node can override and implement such that...

MenuBase.mItems[1]->GetItem(0); Does what you are trying to do. There are other ways to do this but this one is simple and I think you will find it easy to implement.

The return value could be Item, or it could be MenuItem whatever you need... here I show it as an Item...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MenuItem
{
    public:
    virtual Item * GetItem(int index = 0); //You could make this pure virtual
}

class Item : public MenuItem
{
   virtual Item * GetItem(int index = 0)
   {
       return *this;
    }
}

class Node : public MenuItem
{
   virtual Item * GetItem(int index = 0)
   {
       //bounds checking...
       return nodeItems[index];
   }
}
Last edited on
This is where C++ starts to fail and ASP.Net + C# starts to shine. lol

As a programmer in today's age you should only be concerned with how to manipulate your lists and menu's, not how to code them from scratch.

Starting from scratch is admirable but it's also a waste of time in most cases. I think that programmers should definitely get a firm grasp on algorithms and basic logic but stuff like this is just not suited for C++. At least not now that it has already been established.
Last edited on
closed account (o1vk4iN6)
C++ has a wide variety of flavor for GUIs, he seems to simply want to learn how to create his own there is nothing wrong with that. He'll know to appreciate the hardwork and thought that went into designing all the wonderful GUI libraries for C++ as well as learning along the way. Anyone can hop on C#, start dragging and dropping controls without knowing how it all comes together.
Last edited on
Anyone can hop onto C# as you say? Have you actually spent time using the language? The only reason why losers like you claim that anyone can just jump in and use it is because of the wonders of Visual Studio and intellisense or that you already knew C++. While it is certainly not as complex in terms of using pointers (although pointers are still possible) and does not share every object oriented feature that C++ shares (multiple inheritence), it still requires some skill to master, especially since the .Net library completely overshadows the STD library.

The only point I was trying to make is that a language like C# is made for the TS' type of application.

There is no best language in the end. There will always be a language that best fits want you want to ultimately accomplish. Otherwise we would all still be using assembly for everything.
Last edited on
Presently your design seem to be like this

MenuBase 
	|
	|
----------
|				|
Node			Item
|					|
|			Function of the Item
|				
List of new menu Node 
                     |
		 Item
		     |
Function o f the item.


IT have to be like this .

			MyNode
					|
_________________________
|											|	
display Item						Display List of the Node 1
|												|
Call function of the Item				Display Item of the Node1
												|
                                                                                     Call the function of the Item


please commit if i am wrong or needs correction
You can always use vertual function and derived class to produce different nodes
Last edited on
odracir123, I agree with blue coder. You're design is wrong not exactly what you want (for what you want*).

Instead of making a Base Menu class, just use a MenuItem for the base. Classes group together like objects, they don't form a kingdom with a random ruler at the top. They should form a hierarchy with a base foundation at the bottom.

The base menu should simply be a Node all by its lonesome with Nodes and Items in its list.
Last edited on
Hi guys, thanks for all the feedback it has been really useful.
xerzi was right in saying I am doing this project for the experience as opposed to needing the finished item for anything in particular.

clanmjc, your solution makes perfect sense in hindsight. Occasionally I railroad myself into a certain implementation and spend too much effort finding awkward work-arounds for what I want.

bluecoder + Vlykarye, I did think of using a Node instead of a MenuBase but faced the same trouble as before which should hopefully be fine now. A combination of this and clanmjc's suggestion should hopefully prove to be a more elegant solution.

Thanks again guys.
Topic archived. No new replies allowed.