CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    5

    Function pointer with different number of arguments

    I am trying to create a pointer to a function that only requires some of the arguments when called and specifies the others at the time the pointer is created. For example, see the code below.

    Code:
    int main() {
        // The function bar only takes one argument -> a
        // b and c are specified as 1 and 2 (respectively) at time of function creation
        int (*bar)(int a) = &foo( int, 1, 2);
    
        cout << bar(3) << endl;
        // should print 3
    
        int (*bar)(int a) = &foo( int, 4, 5);
    
        cout << bar(3) << endl;
        // should print 12
    }
    
    // foo takes 3 arguments, but b and c are only necessary when first called
    int foo(int a, int b, int c) {
       static int d = b + c;
       return a + d;
    }
    Of course, as written this code doesn't compile, but I think it should help you understand what I'm trying to do. I have programmed in lisp and this is very easy to do, I would think C++ should have a way of doing it.

    Thanks in advance for any help.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function pointer with different number of arguments

    It sounds to me like you're looking for boost::bind.

    Basically what it does is create a functor which passes some arguments through to the underlying function, and hides others using values specified at binding time. You can store the functor in a boost::function if you need a variable to hold it; otherwise, just use a template so that the specific type isn't a problem.

    Also, static local variables should be avoided as they create bad habits.
    Last edited by Lindley; September 24th, 2009 at 05:09 PM.

  3. #3
    Join Date
    Sep 2009
    Posts
    5

    Re: Function pointer with different number of arguments

    Quote Originally Posted by Lindley View Post
    It sounds to me like you're looking for boost::bind.
    Thanks, Lindley. I looked up boost::bind and you're right, I think it would solve my problem. Also, it's been a while for me since I've used C++, so I forgot about functors. Unfortunately this is a school project and I can't just add non-standard libraries to my project. It's a graphics project, so I was just trying to find a way to make it more efficient because I would be calling the same function over and over with the same b and c values where only a is changing. For now I just have it calling the function with all three arguments, so it works. But if anybody knows a way to do this with the standard Visual C++ libraries, let me know.

    Quote Originally Posted by Lindley View Post
    Also, static local variables should be avoided as they create bad habits.
    I didn't know there was anything bad about static local variables. But the static was just for the example, the real code is much more complex than that and includes no statics.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function pointer with different number of arguments

    Generally if you have values which only need to be set once and other values that need to be passed every time, the appropriate design is to build a class rather than a simple function. You put the one-time arguments in the constructor and give the object a public method which does the work.

    The problem with statics becomes apparent when you start thinking about multi-threading. The key aspect of thread safety is the use of local variables, since each thread will have its own stack. When you declare a local variable static, then it becomes just as dangerous as a global variable as far as MT goes.

  5. #5
    Join Date
    Sep 2009
    Posts
    5

    Thumbs up Re: Function pointer with different number of arguments

    Thanks for the help, Lindley. I think making the class is the best solution.

    As far as the statics go, you're right; it's obvious how it could be a problem as soon as you think about multi-threading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured