Function Pointers?

This is a discussion on Function Pointers? within the C++ Programming forums, part of the General Programming Boards category; okeydokey, time for another of my weird hypothetical example spawned questions. If i have a pile of functions, func1, someotherfunc, ...

  1. #1
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    Function Pointers?

    okeydokey, time for another of my weird hypothetical example spawned questions. If i have a pile of functions, func1, someotherfunc, ... morefuncsthanicancount. that all do different things. and i got my luser...erm... user who enters the name of a function, how do i go about calling what they enter. if they type in func42 i want it to call func42. now my thought on how to do this was a vector of pair<string,function*> so i could search the vector for the right string, and execute the function pointer by address rather than name. now for some problems. how do i pass parameters to these? if they enter func32 X Y how can i pass the X and Y being that i dont know the prototype of functions i will be calling. again my bad idea was to have every function take a void pointer and type cast it over to whatever struct of parameters that particular function wanted. Before i waste the next 90 hours of my life trying to code this thing, i want to know what i have stated that clearly wont work, or is terribly flawed, or will make me just scrap the whole project, and how to get the address of a function to store to a vector. thanks for feeding my imagination.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I can't see why you would want the user just to enter something like 'func42' or whatever...Um why do you want to do this? Can you think of one useful program that does (would do this)? Ok sorry...Just wondering...Maybe I missed your point
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    it would be for use in my new toy. im trying to provide a user with a scripted config file. they could script addons to the program. i could just parse the config file and allow new functions to be added as necessary.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First, I'd use a map<string, function*>... Its an associative container, so you'd simply have (for example):

    Code:
    map<string, void (*)(void)> func_map;
    // Populate...
    func_map["func42"]();
    Second, to have a function pointer, you must know the prototype. void* is one way to pass different things. You could also just pass them the string of parameters, and have them parse that string themselves (a better idea).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    (a better idea)
    Especially if this is for scripting.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21