Thread: Typecasting a void* to a function pointer

  1. #1
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189

    Typecasting a void* to a function pointer

    Is there any way to do this?

    In a program I'm writing there is a class called "command", which is intended to be an element in a list of function calls; it is supposed to contain a pointer to a general function along with the arguments to be used. I want to use a void* for the function pointer variable, for generality and in order to use function arguments that haven't been defined when the command class compiles.

    However, when it's time to call the function, I can't seem to convert the void pointer to a function pointer. I've tried various ways of doing this; none of them work - I get a compiler error saying it can't do the conversion (both when I convert to the void* in the first place and in going back to the function pointer).

    I suspect from the errors I'm getting that typecasting function pointers is simply not allowed. If it's permissible and I'm just doing something wrong, though, or if you know another way to accomplish the same goal, any advice would be appreciated. I can post example code if necessary.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    I've actually been using that site as a reference for some more basic function pointer stuff but didn't see anything directly related to typecasting. Does the callback/functor material actually address this issue? I didn't bother looking at it before. (It looks like it might...)

    Edit: It seems to have a solution of sorts but it's far too unwieldy for my purposes. I'm just going to ditch the void pointers and try a different approach.
    Last edited by Procyon; 01-14-2004 at 07:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM