|
Hi all,
For my current project[^] I need to use a function pointer to allow a QueueHandler to execute the task of a QueueObject.
I know about what a function pointer is, no worries I searched the net[^] a bit.
I got one question:
-- Is there a possibility to point towards a function without knowing what it exactly returns, and more important without any knowledge of the count and type of the parameters?
In the end it should be like the delegate I know from the .NET side of multithreading.
Thanks for all suggestions in advance.
[Edit: To the univoter who stroke down this question:
Either explain why you have down-voted or go and die in a f*cking fire you moronic ********]
modified 21-Mar-13 11:55am.
|
|
|
|
|
I don't think you can do this in C/C++. How would you pass the correct parameters if you don't know the type and count, and how would you handle whatever it returns? I guess the only possible way round it would be to send a void* to some memory area containing the required parameters, or use the void func(...) type definition. But you still need to know the input and output types in the code that calls the function.
Use the best guess
|
|
|
|
|
Thank you for your reply.
Another idea which came to my mind: Can I use a array of pointers of an unknown type as arguments for the function?
In this case I'd be able to force the user of the tool to just give this array as a parameter.
This approach would look like
int (*DoTaks)(void[]);
Please correct me if I am wrong.
|
|
|
|
|
Marco Bertschi wrote: Can I use a array of pointers of an unknown type as arguments for the function?
I would say, yes, you can do that, but it's dangerous because it's not type-safe. This type of code would be filled with casts and could be very difficult to get right.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thank you for your answer, I understand the problem.
Is there any other way to get something like a delegate in native C++?
|
|
|
|
|
Even for a managed delegate you need to define the paramter types and return type. I would suggest defining a class or struct containing all the parameters you need to pass and additionally a kind of type that tells the receiving function which members it needs to use. Then you pass just that struct as parameter.
|
|
|
|
|
Freak30 wrote: I would suggest defining a class or struct containing all the parameters you need to pass
There does the problem start. I don't know which parameters are needed because I do not define the method which is executed. It is needed as a part of this project[^] (as I have mentioned in the OP).
Thank you for your help anyways.
|
|
|
|
|
After viewing your link, i couldn't help but wonder if a deque would be better. (aka work stealing - google Cilk)
All tasks take work from the front of its own queue, but an idle task takes work (steals) from another by removing items from the back of its queue.
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
|
|
|
|
|
Yes you could do that (you need int (*DoTaks)(void*); notation), but you still have the problem that any mistakes with the parameters cannot be spotted by the compiler. If you are happy to live with that risk then go ahead; a small prototype should help to clarify the issues you may face. Something like:
struct aStruct
{
int value;
char someText[12];
};
int func1(int pvParameters[3])
{
return 1;
}
int func2(aStruct* paStruct)
{
return 2;
}
void DoTest()
{
int myArray[] = { 1, 2, 3 };
aStruct myStruct = { 5, "data" };
int (*DoFunc)(void*);
DoFunc = (int (*)(void*))func1;
int r = DoFunc((void*)myArray);
DoFunc = (int (*)(void*))func2;
int r2 = DoFunc((void*)&myStruct);
}
Use the best guess
|
|
|
|