Telling a function how many parameters it receives
Sep 22, 2015 at 1:17pm UTC
Hello. I'm having problems with a procedure:
void queue_destroy (queue_t* queue, void destroy_data(void *))
What I ought to do is: if queue_destroy receives just the queue, delete the queue as I know how. If I receive also a certain void*, I ought to call destroy_data instead.
How can I have the function queue destroy ask itself if it's receiving two parameters?
EDIT: destroy_data works like free().
Last edited on Sep 22, 2015 at 2:19pm UTC
Sep 22, 2015 at 2:37pm UTC
Sounds like you would want to do that before you send it to your function.
Sep 22, 2015 at 2:43pm UTC
What you need to do is change the second argument to an optional function pointer.
1 2
// Prototype
void queue_destroy (queue_t* queue, void (*destroy_data)(void *) = nullptr );
Now you can test if the second argument was supplied by comparing it to nullptr.
1 2 3 4 5 6
// Implementation
void queue_destroy (queue_t* queue, void (*destroy_data)(void *))
{ if (destroy_data != nullptr )
(*destroy_data) (queue);
}
Topic archived. No new replies allowed.