Thread: Binding a function argument

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Binding a function argument

    Hi I have a problem with a plugin I am making. The program I am plugin into only allows me to use functions with no arguments, but I want to use the same function for an unknown(at compile time) number of different callbacks all with different arguments.

    I want different user information to pass to the function when the user initiates the calling of this function, depending on which part of the program called the function. The user information will be stored in a configuration file.

    I hope this is clear enough for people to work out what I mean. In essence I can try and put it as this pseudo code:

    Code:
    void func (char *arg) { 
        /*...do stuff...*/
    }
    
    void newfunc() = bind(&oldfunc, arg)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I hope this is clear enough for people to work out what I mean.
    Not really, but I get the feeling that a function object would be better suited to what you're trying to accomplish. You can also get a variable number of callbacks with a collection of handles (but please don't use varargs).
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    I dont know exactly what you mean, but can't you just use the functions with no arguments and use a global variable as paramater info?

    Code:
    name = "itsme";
    
    void func()
    {
      cout << name << endl;
    }

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Unfortuanatly function objects wouldn't work as the program I'm plugin into won't accept them (it crashes). Maybe I should try to explain with some example code.

    Example of my code
    Code:
    void func(char *text) {
      cout << text << endl;
    }
    
    functions funcArray[3];
    funcArray[0] = func;
    funcArray[1] = func;
    funcArray[2] = func;
    
    functions getFuncs ()
    {
      return funcArray;
    }
    Example of the other programs code
    Code:
    /* When user clicks on a menu item */
      callable = getFuncs()[itemID];
      callable();
    Obviously the above wouldn't work as no argument is being given to func. I can't tell which menu item triggered the function being called so I can't set a global variable.
    Last edited by nectodn; 11-20-2006 at 01:58 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    void func(char *text) {
      cout << text << endl;
    }
     
    void func0(void)
    {
       func("text0");
    }
    void func1(void)
    {
       func("text1");
    }
    void func2(void)
    {
       func("text2");
    }
    functions funcArray[3];
    funcArray[0] = func0;
    funcArray[1] = func1;
    funcArray[2] = func2;
     
    functions getFuncs ()
    {
      return funcArray;
    }
    /* When user clicks on a menu item */
      funcArray[itemID]();
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    My problem is that the exact text will not be known at compile time. The text will be gleaned from a file once the program is running and is subject to change. I will not even know how many functions I will use until run-time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM