Create and execute the void function stack

I am trying to save a vector (or stack) of functions. The idea is that I have a number of functions that add and remove widgets to the main window. I use the timer alarm and whenever the alarm is called, I call the function at the top of the function stack.

That way my functions will always be of type void. My problem / misunderstanding is how to handle stl :: stack from void functions and how to execute this function?

class InstructionScreen { std::stack <void*> instructionSteps; // is this how I declare a stack of functions void runTimerEvent() { if ( !instructionSteps.empty() ) { // call the function at the top of the stack // how do I call the function? (*instructionSteps.top()); // is that how? instructionSteps.pop(); } } void step1() { // here I create some widgets & add them to the main window } void step2() { // delete widgets from window // create some different widgets & add them to the main window } void buildStack() { instructionSteps.push( (&step1()) ); // is this correct? instructionSteps.push( (&step2()) ); } }; 
+4
source share
6 answers

A void* A pointer is not a pointer to a legal function. It should be void (*)() , which can be done better with typedef void (*stack_function)() .

 std::stack<stack_function> instructionSteps; 

To insert something into it, you do not call the function (as you do with step1() ), and you, of course, do not take the return address (which is somehow invalid), as you do with &step1() , you simply use only function name:

 instructionSteps.push(step1); // the & can be omitted for free functions instructionSteps.push(&step2); // equivalent to the above, only a different function 

To call stuff from the top of the stack, you really need to make a call:

 (*instructionSteps.top())(); // you missed that -- ^^ 

The difference may also be omitted for reasons that would take too long to explain here, the search for SO. :)

 instructionSteps.top()(); 
+3
source

The syntax for a static function pointer looks like this:

 void (*FuncPtr)(); 

For a member pointer, you should use this syntax:

 void (class::*FuncPtr)(); 

If your functions do not require functions to be member functions, this is much cleaner. Once you figure out what functions you need, the easiest way to type these functions is:

 typedef void(*FuncPtrType)(); typedef void(Class::*MemberFuncPtrType)(); 

Now you can simply declare a stack with function pointers as follows:

 std::stack <FuncPtrType> funcPtrStack; std::stack <MemberFuncPtrType> memberFuncPtrStack; 

To get a pointer to a function, you simply use "&" as you would like to get the address for any other data type in C ++:

 FuncPtrType funcPtr = &staticFunc; // Somewhere "void staticFunc()" is defined MemberFuncPtrType memberFuncPtr = &Class::MemberFunc; // Somewhere void "Class::MemberFunc()" is defined 

To actually call function pointers, you must use the "*" operator to return data from a pointer (like any other data type in C ++). The only difficult thing is that for member functions they need a pointer to a class, which makes it very inconvenient to use. This is why I recommended using static functions to get started. Anyway, here is the syntax:

 (*funcPtr)(); // I just called a function with a pointer! (this->*memberFuncPtr)(); // I just wrote some ugly code to call a member function 

Having shown all this, the following code should now make sense:

 std::stack <MemberFuncPtrType> memberFuncPtrStack; // Declaring the stack memberFuncPtrStack.push( &Class::MemberFunc ); // Pushing a function (ClassPtr->*memberFuncPtrStack.top())(); // Calling the function with ClassPtr 
+2
source

Declare typedef and make vector / stack from this:

 typedef void (*funcptr)(); std::stack<funcptr> instructionSteps; 

Using:

 instructionSteps.push(&step1); instructionSteps.push(&step2); 

See the demo here . Execution:

 instructionSteps.top()(); 
+1
source

Tip. Use the Boost.Function function, it is much simpler. It will not only store functions with exactly the right type, but everything else that can be called in the same way.

 std::stack<boost::function<void()> instructionSteps; int foo() { return 42; } instructionSteps.push(foo); // Close enough - return value will be discarded. 
+1
source
 typedef void (*fptr)(); class InstructionScreen { std::stack <fptr> instructionSteps; 
0
source

I would typedef a function pointer to make your life easier:

 typedef void(*voidFunctionPointer)(); // Assuming here that the functions take no arguments. std::stack<voidFunctionPointer> instructionSteps; // This is very different from <void*>. // The latter is merely a stack of void pointers. 

One way to call the top function is:

 voidFunctionPointer functionToCall = instructionSteps.top(); functionToCall(); 

If you want to do this without an additional declaration, I think this should work. Please correct me if I am wrong.

 instructionSteps.top()(); 

To create a stack, simply use the function name without any trailing brackets.

 instructionSteps.push(step1); instructionSteps.push(step2); // ... 
0
source

All Articles