是否有一个方法/模式/图书馆做这样的东西(在伪码):

task_queue.push_back(ObjectType object1, method1);
task_queue.push_back(OtherObjectType object2, method2);

因此,我可以做的 的东西 如:

for(int i=0; i<task_queue.size(); i++) {
    task_queue[i].object -> method();
}

因此,它将呼叫:

obj1.method1();
obj2.method2();

或是一个不可能的梦想吗?

如果有一个方式来增加的一些参数的话--这将是最好的。

道格*T请看看这个 优秀的答案!

Dave Van den Eynde's版本的工作。

有帮助吗?

解决方案

是的,你会想提升相结合::绑定的boost ::功能的其本身的强大的东西。

现在这个版本编译,感谢光荣!

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <vector>

class CClass1
{
public:
    void AMethod(int i, float f) { std::cout << "CClass1::AMethod(" << i <<");\n"; }
};

class CClass2
{
public:
    void AnotherMethod(int i) { std::cout << "CClass2::AnotherMethod(" << i <<");\n"; }
};

int main() {
    boost::function< void (int) > method1, method2;
    CClass1 class1instance;
    CClass2 class2instance;
    method1 = boost::bind(&CClass1::AMethod, class1instance, _1, 6.0) ;
    method2 = boost::bind(&CClass2::AnotherMethod, class2instance, _1) ;

    // does class1instance.AMethod(5, 6.0)
    method1(5);

    // does class2instance.AMethod(5)
    method2(5);


    // stored in a vector of functions...
    std::vector< boost::function<void(int)> > functionVec;
    functionVec.push_back(method1);
    functionVec.push_back(method2);

    for ( int i = 0; i < functionVec.size(); ++i)
    {         
         functionVec[i]( 5);
    };
    return 0;
};

其他提示

由于用C++不支持异质的容器,你的目的将需要有一个共同的基础(所以你可以得到具有一个容器指向这个基类)。

class shared_base {
     public:
     virtual void method() = 0; // force subclasses to do something about it
};

typedef std::list<shared_base*> obj_list;

class object : public shared_base {
     public:
     virtual void method() { methodx(); }
     private:
     int methodx(); 
};

// ...
list.insert(new object);

// ...
std::for_each(list.begin(), list.end(), std::mem_fun(&shared_base::method));

你是想要实现的 好莱坞原则 否则,被称为反转的控制(并同时,普曼的错误处理)?

看起来两个 观察员访客 模式--他们可能会感兴趣。

我搅打东西了。

#include <vector>
#include <algorithm>
#include <iostream>

template <typename ARG>
class TaskSystem
{
private:
    class DelegateBase
    {
    public:
        virtual ~DelegateBase() { }
        virtual void Invoke(ARG arg) = 0;
    };

    template <typename T>
    class Delegate : public DelegateBase
    {
    public:
        typedef void (T::*Func)(ARG arg);

    private:
        Func m_func;
        T* m_object;

    public:
        Delegate(T* object, Func func)
            : m_object(object), m_func(func)
        { }

        virtual void Invoke(ARG arg) 
        { 
            ((*m_object).*(m_func))(arg);
        }
    };

    typedef std::vector<DelegateBase*> Delegates;
    Delegates m_delegates;

public:
    ~TaskSystem()
    {
        Clear();
    }

    void Clear()
    {
        Delegates::iterator item = m_delegates.begin();

        for (; item != m_delegates.end(); ++item)
        {
            delete *item;
        }

        m_delegates.clear();
    }

    template <typename T>
    void AddDelegate(T& object, typename Delegate<T>::Func func)
    {
        DelegateBase* delegate = new Delegate<T>(&object, func);
        m_delegates.push_back(delegate);
    }

    void Invoke(ARG arg)
    {
        Delegates::iterator item = m_delegates.begin();

        for (; item != m_delegates.end(); ++item)
        {
            (*item)->Invoke(arg);
        }
    }

};

class TaskObject1
{
public:
    void CallOne(const wchar_t* value)
    {
        std::wcout << L"CallOne(): " << value << std::endl;
    }

    void CallTwo(const wchar_t* value)
    {
        std::wcout << L"CallTwo(): " << value << std::endl;
    }
};

class TaskObject2
{
public:
    void CallThree(const wchar_t* value)
    {
        std::wcout << L"CallThree(): " << value << std::endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    TaskSystem<const wchar_t*> tasks;

    TaskObject1 obj1;
    TaskObject2 obj2;

    tasks.AddDelegate(obj1, &TaskObject1::CallOne);
    tasks.AddDelegate(obj1, &TaskObject1::CallTwo);
    tasks.AddDelegate(obj2, &TaskObject2::CallThree);

    tasks.Invoke(L"Hello, World!\n");

    return 0;
}

也许你可以考虑用另一种方式:

for(int i=0; i<task_queue.size(); i++) {
    task_queue[i].method(task_queue[i].object);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top