Jump to content
  • Advertisement
Sign in to follow this  

Scheduling system not accepting function pointer

This topic is 4527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have implemented a scheduling system into my appication so I can register functions that are to be run at a specific time interval. The scheduler consists of "tasks" that store the time interval (double) and a function pointer (typedef void(f*)(void)) to the function I want to run within the interval.
class CScheduler

	// Constructor.

	// Destructor.

	// Adding a task.
	void AddTask(double aTimeIntervalInMicro, f* aTaskFunction);

	// Removing a task.
	void RemoveTask(int aIndex);

	// Getting system time.
	DWORD GetTime(); 

	// Updating scheduler.
	void Update();

	// Getting singleton instance.
	static CScheduler* Instance();


	// List of registered tasks.
	std::vector<CTask*> iTasks;

	// Singleton instance.
	static CScheduler* iInstance;

class CTask

	// Constructor.
	CTask(double aTimeIntervalInMicro, f* aTaskFunction)
		iTimeInterval = aTimeIntervalInMicro;
		iTaskFunction = aTaskFunction;

	// Time interval for task.
	double iTimeInterval;

	// Task to trigger.
	f* iTaskFunction;

	// Last update for taks.
	double iLastUpdate;

When I want to register a task in the scheduler, I cannot really figure out how to type in the function. I want to register functions from my system classes, which are all singleton, and the functions I want to call are non-static members. Here is what I attempted, but it did not quite work:
iScheduler->AddTask(1.0, CGui::Instance())->Update());

The compiler (Visual Studio 2005) tells me that I should type &CGui::Update as the function pointer, but that will not work as the update function is a non-static member. Without having to make every single member function of every class I have static, since I want to be able to register any function, what could I do? Has anybody imlemented a scheduling system and managed to make a function pointer or something similar? Then I am hoping you can share some tips.

Share this post

Link to post
Share on other sites
Boost's function and bind libraries will make your life infinitely easier here; use them. However, if you insist on doing it the hard way, take a look at this. It will help you through everything you need for function pointers and method pointers.

Note that if you don't use boost, you'll have to store a point to the object as well as a pointer to the method in order to use a method pointer.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!