Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我如何才能boost::绑定一个抽象重写方法,使子';调用的是什么方法?_C++_Boost - Fatal编程技术网

C++ 我如何才能boost::绑定一个抽象重写方法,使子';调用的是什么方法?

C++ 我如何才能boost::绑定一个抽象重写方法,使子';调用的是什么方法?,c++,boost,C++,Boost,我正在编写一个具有抽象回调的基类。像这样: class ValueListener { public: ValueListener(); void registerPoint(ValuesSource &mgr, bool create=true); void valueReceived( QVariant value ) = 0; /* slot */ QString valueName() = 0; }; 重写类应实现它们希望对接收的值执行的操作。但

我正在编写一个具有抽象回调的基类。像这样:

class ValueListener
{
public:
    ValueListener();
    void registerPoint(ValuesSource &mgr, bool create=true);
    void valueReceived( QVariant value ) = 0; /* slot */
    QString valueName() = 0;
};
重写类应实现它们希望对接收的值执行的操作。但是
ValueListener
本身负责注册回调:

void ValueListener::registerPoint( ValuesSource& mgr, bool create ) {
    ValueSourceInfo* info = mgr.getPoint(valueName(), create);
    if(info) {
        // Connect the callback
        info->valueChanged.connect( boost::bind( &ValueListener::valueReceived, this, _1 ) );
    }
}

但是很明显,无论是
this
还是
&ValueListener::valueReceived
都不是应该接收值更新的对象-重写类应该是。那么,如何在不知道的情况下绑定被重写的方法呢?

事实证明,这样做可能是一个有缺陷的想法。相反,我创建了两个方法,一个是普通方法,一个是私有方法:

class ValueListener
{
public:
    ValueListener();
    void registerPoint(ValuesSource &mgr, bool create=true);
    void valueReceived( QVariant value ) = 0;
    QString valueName() = 0;
private:
    void valueReceivedPrivate( QVariant value ) {valueReceived(value);}; /* slot */
};
我在私有方法上使用了connect:

void ValueListener::registerPoint( ValuesSource& mgr, bool create ) {
    ValueSourceInfo* info = mgr.getPoint(valueName(), create);
    if(info) {
        // Connect the callback
        info->valueChanged.connect( boost::bind( &ValueListener::valueReceivedPrivate, this, _1 ) );
    }