How to pass a callback function (with arguments) as a parameter of another function

I know how to pass a callback function as a parameter of a class constructor function, and store its reference in the class...
(hope that made sense)

1
2
3
4
5
6
7
8
9
10
11
12
13
public:
	using Callback = void();

	Game (std::vector<GameObject> &gameObjects,
		  Callback &updateCB): m_gameObjects(gameObjects), m_gameUpdate(updateCB) {
		// Game Setup
	}

private:
        std::vector<GameObject> &m_gameObjects;

	// Callbacks
	Callback &m_gameUpdate;


...but I'm trying to find out how to pass a callback function that expects 1 or more arguments:

 
void someCallback(int a); 


I asked a related question a few days ago, and using templates was suggested for more flexibility, but if I change "using Callback = void();" for "template<typename func_type>", I get a compiler error:

 
'Callback' does not name a type // Can't find it, i did something wrong. 


I put the template definition right below the public keyword, maybe I'm using it wrong or placing it in the incorrect place, and is there any other way to accomplish this?
Last edited on
You may be interested in std::function from <functional> http://en.cppreference.com/w/cpp/utility/functional/function
Don't store an alias to the callback, store a copy.

Suppose you get this working. m_gameUpdate takes an arbitrary number of parameters. How do you expect to call it? How are you going to decide what arguments to feed it?
Last edited on
Thanks Cire and Kevin for your answers. I got a better understanding now.
Last edited on
Topic archived. No new replies allowed.