Thread: simple pointer-to-func question.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is not clear?
    Perhaps it can be "cleared" up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think you missed the question. If the type is static, then you are right that there is no need for a "void pointer to function" -- you can use a regular function pointer. If the type is dynamic however, then you cannot use a regular function, and you cannot use a template, since both of these require you to know the type compile time. Therefore, another solution is called for.

    Note I am using static in this context to refer to what is know at compile time. Dynamic, in contrast, would refer to everything that cannot be determined before the program is run.

    Your template solution still deals with static types, since T must be known at compile time. In contrast a void * and its function pointer equivalent (which is what the OP asked about) does not know the type that it points to. In order to dereference it, run time data must be supplied to provide that type, and the pointer must be cast before being applied.


    Now of course nobody really wants a void pointer, since a void pointer can point to any type, and in any real world problem there is only a finite set of possible types that is can stand for. What is really wanted is a pointer that can point to a finite set of data. This is why polymorphic classes are the preferred solution to such problems.
    Similarly here what is really wanted is not a void function pointer, but a way for a pointer to be able to point to a finite but different set of possible functions. Again the preferred solution is polymorphic "functors" -- classes with overloaded operator().
    Last edited by King Mir; 08-05-2008 at 07:26 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the question is rather broad. there are many ways that you could possibly do such a thing, but few that would be genuinely useful.

    in the code you posted, you could alter f1 to accept type void *, and then cast the argument to string when outputting to stdout.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    Now of course nobody really wants a void pointer, since a void pointer can point to any type, and in any real world problem there is only a finite set of possible types that is can stand for. What is really wanted is a pointer that can point to a finite set of data. This is why polymorphic classes are the preferred solution to such problems.
    Similarly here what is really wanted is not a void function pointer, but a way for a pointer to be able to point to a finite but different set of possible functions. Again the preferred solution is polymorphic "functors" -- classes with overloaded operator().
    And of course, a finite set of types is usually represented by templates. I get what you mean, and I agree with your preferred solution, as an alternative to the void* mess.
    It's a good example of how templates and design can be used to side-step the whole void* mess, which, I admit, was what I was trying to demonstrate - that templates could be used instead of the void*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    And of course, a finite set of types is usually represented by templates.
    As a matter of fact, there is no defined limit on the number of types that can be represented by templates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM