我怎样才能提升::绑定一个抽象的覆盖方法,以便调用子方法?

时间:2016-01-06 12:49:45

标签: c++ boost

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

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::valueReceivedsys.path都不是应该接收值更新的东西 - 覆盖的类应该。那么如何在不知道的情况下绑定被覆盖的方法呢?

1 个答案:

答案 0 :(得分:0)

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

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 */
};

我在私有方法上使用了连接:

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 ) );
    }