English 中文(简体)
提升:组合和虚拟职能
原标题:boost::bind and virtual functions
  • 时间:2012-05-17 11:45:14
  •  标签:
  • c++
  • boost

我有以下接口:

template <class T>
class AbstractTask {
public:
    virtual void BindTaskCompleted(AbstractTask<T> &bindedTask)=0;
    virtual void Join(TaskResult<T>)=0;
};

并且:

template <class T>
class SlaveTask: public AbstractTask<T> {
public:
    typedef boost::function<void(TaskResult<T>)> joinFunction;

    void BindTaskCompleted(AbstractTask<T> &bindedTask)
    {
        /////////////WORK OK//////////////////////////////  
        //bindedTask.Join(result);

        /////////////COMPILATION ERROR/////////////////////
        slaveTaskCompletionFunction=boost::bind(&AbstractTask<T>::Join,bindedTask,result);
    }

     void Join(TaskResult<T> r)
    {
        slaveTaskCompletionFunction(r);
    }

private:
    joinFunction slaveTaskCompletionFunction;
    TaskResult<T> result;

};

I m试图将执行摘要Task的虚拟Join方法学捆绑起来,以促进:与同一签名相操作。 助推:Bbind投掷了77个汇编错误,我看不出原因。

I thought at first that boost::bind couldn t be use with virtual method but this doesn t seem to be the case: Virtual function and boost bind strange behavior

事先感谢你们的帮助!

Thomas

最佳回答

问题是,你把所有论点都束缚在后面,但<条码>boost:功能,以及你如何重新使用条码表示它想有1个论点。

修改如下。

slaveTaskCompletionFunction=boost:bind(&AbstractTask<T>:Join,&bindedTask,_1);

Edit: Also, you had a slicing problem when you passed in bindedTask by reference. You either have to pass it in by pointer, or use boost::ref; otherwise, boost::bind attempts to make a copy of the AbstractTask<T>, and will end up only copying the interface.

问题回答

要求:

slaveTaskCompletionFunction=boost::bind(&AbstractTask<T>::Join, &bindedTask, _1);

无论是否受到约束 任务:

   BindTaskCompleted(const AbstractTask<T> &bindedTask) 

or use boost::ref to bind it:

   boost::bind(&AbstractTask<T>::Join,boost::ref(bindedTask),result);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签