vector of function objects

This is a discussion on vector of function objects within the C++ Programming forums, part of the General Programming Boards category; Hi all, I want to be able to do this: Code: std::vector<???> vec; vec.push_back(boost::bind(&testClass::run,t)); vec.push_back(boost::bind(&testfunc)); for(int i = 0; i ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    40

    vector of function objects

    Hi all, I want to be able to do this:
    Code:
    std::vector<???> vec; 
    vec.push_back(boost::bind(&testClass::run,t));
    vec.push_back(boost::bind(&testfunc));
    
    for(int i = 0; i <vec.size(); ++i) // invoke all function objects in vec...
    {
    	vec[i](); 
    }
    How do i accomplish this? All functions objects is invoked in the same way by the operator () but I have no idea how I make a vector of such objects, I tried first with a void (*)() but of course didn't it work, as I thought I have one ugly solution and it is to make a abstract class and then make a virtual method and use polymorphism.

    Thanks in advance.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Use an extra abstraction:
    Code:
    class CFunctorBase
    {
    public:
        virtual void operator () = 0;
    };
    
    template<typename T> class CFunctor: public CFunctorBase
    {
    public:
        FunctorBase(T Functor): m_Functor(Functor) { }
        virtual void operator () { m_Functor(); }
    private:
        T m_Functor;
    };
    
    template<typename T> T* CreateFunctor(T* Functor)
    {
        return Functor;
    }
    
    std::vector< boost::shared_ptr<FunctorBase> > v;
    v.push_back( CreateFunctor(new Functor(boost::bind(...)) );
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    I've never used it myself, but I'm assuming like std::vector< boost::function< void ( * )( ) > > should work.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    void (*) () doesn't work for member function pointers!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    boost::function<void ()> seems appropriate.
    Tutorial

    gg

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    void (*) () doesn't work for member function pointers!
    Well I know that. I just assumed boost::function would specialize it appropriately.

    I guess not.

    Doesn't seem very handy, though, IMO.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    I fixed it, thanks for your answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

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