Thread: Storing a series of function calls

  1. #1
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59

    Storing a series of function calls

    So I'm trying to write some sort of system to store a series of function calls.

    The goal is sort of unclear, but I was thinking maybe I could have a series of member function calls stored in an object which would effect the object, and could add or remove calls from this list at runtime.

    I just spent a little while trying to write a generic "function call" object that will store a function pointer of variable argument length that I can call.

    And then I was figuring I could put them into a vector to be able to call them at runtime.

    So I started to wonder if this was even worth doing, and I'm also wondering if there's already a data structure in the STL that might be more suited for what I'm trying to do.

    Thanks for any advice, and if you need to me clarify anything let me know

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There certainly may be ways to do this, e.g., by storing function pointers or function objects, but why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    I didn't want to get too specific to what I'm working on, but it's some game system architecture.

    The initial reason I wanted to work on this was the thought of dynamically storing a series of different effect/behavior functions onto particles in a particle system.

    So I figured that maybe it could be a clean way to do that, for example if one object only needs to call 2 functions each time it updates, but another needs to call 20.

    And I'm just getting into C++/OOP so it is also kind of an educational experience.

    I'm sure it might be a bit faster to just hard code this stuff but I wanted to try out some component based ideas for it.

  4. #4
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    And I was wondering if anybody had ever tried/considered this, or if maybe using a map or something would be loads faster

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by inequity
    The initial reason I wanted to work on this was the thought of dynamically storing a series of different effect/behavior functions onto particles in a particle system.

    So I figured that maybe it could be a clean way to do that, for example if one object only needs to call 2 functions each time it updates, but another needs to call 20.
    I am just guessing, of course, but a hierarchy of effect/behaviour classes might work. A virtual member function, e.g., an overload of operator() or one named apply, could then be passed these objects to apply the effect/behaviour to. If you want to store these, or (smart) pointers to these, in a container, it certainly could be done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a Series of Times e.g. 1, 1.5, 2, 2.5 seconds
    By bengreenwood in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2009, 09:53 AM
  2. why does c++ do function calls like this?
    By sndhearn in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 03:17 AM
  3. Function calls
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2003, 02:50 PM
  4. Function calls
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2002, 04:01 PM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 12:54 AM