0
\$\begingroup\$

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)

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

1 Answer 1

2
\$\begingroup\$

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .