Thread: Function pointers again

  1. #1
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    Function pointers again

    How do you go about making a vector of pointers to functions? They might be member functions of different classes or just globals. I was thinking something like:

    vector < *returntype (arg, arg, arg, arg) > FooVector ;

    Oh well, if I get these the dynamic menu will be worth it
    Ph33r the sphericalCUBE

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    you can always typedef it...

    Code:
    typedef void(*pFunc)( int x ) function_pointer;
    
    vector<function_pointer> functions;
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    > you can always typedef it

    I think it should be like this:

    Code:
    typedef void(*function_pointer)( int ) ;
    
    vector<function_pointer> functions;

  4. #4
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    oops, forgot my code tags *bash head emoticon does not exist on this server (404)*.

    Still think it needs the name of the type tacked on the end, but yeah, only argument types in the parentheisisisis.

    ooops.

    Will that work on all functions or just globals?
    Ph33r the sphericalCUBE

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <vector>
    #include <iostream>
    
    void Alpha()
    {
    	std::cout << "Alpha" << std::endl;
    }
    
    void Beta()
    {
    	std::cout << "Beta" << std::endl;
    }
    
    
    int main(void)
    {
    	typedef std::vector<void(*)(void)> FunctionVector;
    	FunctionVector v;
    
    	v.push_back(&Alpha);
    	v.push_back(&Beta);
    
    	FunctionVector::const_iterator i;
    	for(i = v.begin();i < v.end();++i)
    		(*i)();	
    }

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Dante's typedef is the correct one. But your biggest problem is that this method won't work for member functions. I would make a functor and form a vector of those.

    This would be simple, except for the fact that you want to be able to stuff different classes in there. To do that, you'll have to use pointers, templates, and polymorphism.

    Something like this:
    Code:
    #include <iostream>
    #include <vector>
    
    int func(int i) { return i + 10; }
    
    class MyClass
    {
    public:
      int func(int i) { return i + 20; }
    };
    
    class MyClass2
    {
    public:
      int func(int i) { return i + 30; }
    };
    
    struct FunctorBase
    {
      virtual int operator()(int) = 0;
    };
    
    template<typename T>
      struct MyMemberFunctor: FunctorBase
      {
        typedef int (T::*pMember) (int);
    
        T* object;
        pMember method;
    
        MyMemberFunctor(T& _object, pMember _method)
        : object(&_object), method(_method)
        {}
    
        int operator()(int i)
        {
          return (object->*method)(i);
        }
      };
    
    struct MyFunctor: FunctorBase
    {
      typedef int (*pFunction)(int);
      
      pFunction func;
    
      MyFunctor(pFunction _func)
      : func(_func)
      {}
    
      int operator()(int i)
      {
        return func(i);
      }
    };
    
    int main()
    {
      typedef std::vector<FunctorBase*> FunctorVector;
      FunctorVector vec;
    
      MyClass mc;
      MyClass2 mc2;
    
      // we have to use dynamic allocation for polymorphism
      vec.push_back(new MyMemberFunctor<MyClass2>(mc2, &MyClass2::func));
      vec.push_back(new MyFunctor(&func));
      vec.push_back(new MyMemberFunctor<MyClass>(mc, &MyClass::func));
      
      FunctorVector::iterator itr;
    
      for(itr = vec.begin() ; itr != vec.end(); ++itr)
      {
        FunctorBase* f = *itr;
        std::cout << (*f)(10) << std::endl;
      }
    
      // cleanup
      for(itr = vec.begin(); itr != vec.end(); ++itr)
        delete *itr;
    
      return 0;
    }
    Note that all the functions have to be declared with the same return type and parameters.
    Last edited by thefroggy; 11-09-2003 at 04:15 PM.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Another idea would be to associate each function with a void *, and pass that void* in as a parameter to each call.

    Then you could use static member functions that call nonstatic member functions via a pointer.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    void * ? pointer to a void function or what?
    Im comparatively new to this stuff...

    and what is a functor?

    thanks for your help.
    Ph33r the sphericalCUBE

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    A functor is a function object. It's basically a class with operator() defined that can be used in place of a function pointer.

    void* is a pointer that can point to any data. You use it to pass an object pointer into a static method which then casts the void pointer back to its object type and calls the appropriate member function on it.

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, 09: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, 01: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, 04:22 PM