# C++ — How could I create a list that contains functions, and would either run the top or iterate through and run all?

I'm not sure how to phrase this to be perfectly accurate to my goal implementation

I need a dynamic scene/menu system. I'm going to create a base class and inherit in two other classes specific to either rendering a scene or controlling a menu chain. From here I'll be creating functions with specific draw characteristics (such as a specific menu) and run them into my draw function.

What type of container system would work? I need it to be able to both run only the top and run all of the ones loaded in it (I specify because you cant iterate through a stack, from what I'm told)

• This sounds like a stackoverflow question, not SE gamedev – Steven Sep 11 '17 at 0:45
• I was gonna post it there, too, actually – Chris Sep 11 '17 at 0:48
• Cross-posting is a no-no. :) – Almo Sep 11 '17 at 1:27
• I didn't know that, I apologize – Chris Sep 11 '17 at 1:34

Using the STL, you can create a vector of function pointers.

To do that, as I guess you want to do something like object->function() you need all this functions to have the same prototype (parameters type and return type).

Then, you declare a function pointer type like this

<return_type> (<class_name>::*<type_name>)(<parameter_list>);


So, for example:

typedef void (MenuItem::*drawFuncType)();


Then, you just #include <vector> and then declare a vector like

std::vector<drawFuncType> funcVector;


And you fill it using &draw1 for vector's insertion function.

If you had to do it for multiple classes, you can achieve this using templates for the class.

Hope it helps.

• That's literally exactly what I needed to know. Thank you so much! – Chris Sep 11 '17 at 4:05
• Using std::function over function-pointers might be a nicer alternative. – Tyyppi_77 Sep 11 '17 at 4:36
• how does one use std::function? Also, I cant get this to work. The vector wont take that as input; – Chris Sep 11 '17 at 4:50
• What's exactly the error you get? – jjimenezg93 Sep 11 '17 at 5:09
• Please note that there was a typedef missing. Anyway, please provide a description of the errors you're getting. – jjimenezg93 Sep 11 '17 at 5:16