# Function Pointers

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

## Recommended Posts

void * (*TskPtr)(void**)(void**)

Is this a good default function pointer? or should i use a different default function pointer for the tasks ?, or allow a couple of different ones although I have an idea how to do it, it will introduce some overhead in assigning tasks, as the master thread only gets a new task from the global task list if it signals it has nothing to do =]. if anyone has suggestions they will be more than welcomed. The Tasks can be anything the user wants. Also is this a good system? or there is a better way to do it ? Improvements ? Criticism and suggestions welcomed. Note: Am using visual studio 2008 pro, C plus plus - the plus signs don't show :( the core is a dll file linked to the application at runtime Thank you.

##### Share on other sites
Use boost::function + boost::bind. Using those you won't have to worry about defining a sufficiently generic function signature. I can't say enough, their implementation is superb.

##### Share on other sites
If you're using C++ you should create a Task interface (ie. class with no members and only pure virtual functions), and allow users to extend this interface rather than using a function pointer. The Task interface would require users to implement a method called Run. In addition Run does not need parameters, as the user can give their concrete Task a constructor to simulate passing parameters to Run. This system would be much more flexible than a function pointer. (As for using boost, I don't think this is a complex enough use of function pointers to need it).

##### Share on other sites
I don't use function pointers for my task queue; I use an interface (pretty much exactly how sphen_lee described above!):
class IRunnable{public:	virtual ~IRunnable() {}	virtual void Run() = 0;};
This also lets you pass parameters to functions by storing the parameters inside an instance of a Runnable class:
class TaskManager{public:	static void AddToQueue( IRunable& );};class MyTask : public IRunnable{public:	MyTask( int i ) : m_Value(i) {}	virtual void Run()	{		printf( "%d", m_Value );	}private:	int m_Value;};TaskManager::AddToQueue( *(new MyTask(42)) );

##### Share on other sites
You're using C++, yet are still using void* ??

You should be slapped :)

##### Share on other sites
Quote:
 Original post by TheColonialYou're using C++, yet are still using void* ??You should be slapped :)

Yeah sure..... =], what is wrong with void*, since it can point to anything ?? alternative ?

sphen_lee thanks for the advice I will look into it =] it will be more flexible I didn't thought of that =] before.

Hodgman thanks on an idea how to get started becuase iwas about to post asking for example.

fpsgamer I will look into boost libraries, they seem as something i should know how to use since i heard about it for a long time but never check it out now its the time.

Thanks for all replies and suggestions, if any of the people did their multi-core engines and want to share some of their ideas or thoughts, general structures they used, they will be more than welcomed.

Thanks all.

##### Share on other sites
Quote:
Original post by Coder88
Quote:
 Original post by TheColonialYou're using C++, yet are still using void* ??You should be slapped :)

Yeah sure..... =], what is wrong with void*, since it can point to anything ?? alternative ?

That's precisely the problem: it can point to anything! When you use void* you lose any type safety guarantees the compiler can give you. The alternative is to use an interface like Hodgman has shown, which allows users to pass any kind of data without losing type safety.

##### Share on other sites
Quote:
Original post by sphen_lee
Quote:
Original post by Coder88
Quote:
 Original post by TheColonialYou're using C++, yet are still using void* ??You should be slapped :)

Yeah sure..... =], what is wrong with void*, since it can point to anything ?? alternative ?

That's precisely the problem: it can point to anything! When you use void* you lose any type safety guarantees the compiler can give you. The alternative is to use an interface like Hodgman has shown, which allows users to pass any kind of data without losing type safety.

Maybe its me, but sometimes i use void becuase i just don't know the type that will be passed and don't always need the overkilll design of OOP for simple tasks.

Also I think if the void* pointers are used right for the right job and with responsibility than its ok.

##### Share on other sites
Quote:
 Maybe its me, but sometimes i use void becuase i just don't know the type that will be passed and don't always need the overkilll design of OOP for simple tasks.
Do you mean using void* for routines that can support multiple types of parameters? This is where templates should be used.

If you need to actually know the data type, this is where RTTI should be used.

void* is not needed in the above situations.

Quote:
 Also I think if the void* pointers are used right for the right job and with responsibility than its ok.
Agreed. I personally have only needed to use it a few times--Not in any application software or video games, though, as it is never needed.

##### Share on other sites
Quote:
Original post by Crypter
Quote:
 Maybe its me, but sometimes i use void becuase i just don't know the type that will be passed and don't always need the overkilll design of OOP for simple tasks.
Do you mean using void* for routines that can support multiple types of parameters? This is where templates should be used.

If you need to actually know the data type, this is where RTTI should be used.

void* is not needed in the above situations.

Quote:
 Also I think if the void* pointers are used right for the right job and with responsibility than its ok.
Agreed. I personally have only needed to use it a few times--Not in any application software or video games, though, as it is never needed.

I used the void* for the function pointer since when i tired with templates it would not Link since its in a dll file or it would not work as expected. Anyway than i choose to use void* since i knew how to work with them quite well and it allowed me to use many types with a generic signature until the task interface was presented to me. as for the templates for other things such as args etc... i use them since they work very well.

As you mentioned I rarely use the void* since as sphen_lee has said the type safety is lost.

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631368
• Total Posts
2999595
×