Mandate派生函数实现而不是纯粹的

时间:2013-09-20 05:40:55

标签: c++ virtual maya

我有一种感觉,答案是不可能的,至少我在这些问题上找不到任何东西,但我希望会感到惊讶:

我正在继承并扩展由API提供的类,这个扩展依次从以前继承上述基类的各种派生类中继承。 IE: API提供theirBase。 用户通常会实现class myCommand: public theirBase{}; 我把class extendedBase: public theirBase{};放在一起代替。

由于我想要删除的重复模式(纯锅炉板),以及需要特定实现的一些重复步骤,我想明确强制执行。 如果您熟悉Autodesk Maya,那么我已经扩展了MPxCommand。

通常使它们成为纯粹的虚拟化就可以了,这正是我所追求的功能,但是有一个问题,API要求命令注册实现回调样式静态:

static void *creator() {return new myClass;}

所以很明显没有纯虚拟。


基本的派生和功能命令(或​​者从我自己的专业超级MPxCommand派生的命令)看起来像这样:

class myCommand: public MPxCommand{
public:
            myCommand(){};
    virtual ~myCommand(){};

    static void *creator(){ return new myCommand;} // <-- prevents pure virtual

    static MSyntax syntax // M* are provided API objects/types

    virtual MStatus doIt(const MArgList&);
    virtual MStatus redoIt();
    virtual MStatus undoIt();
protected:
    boilerPlate();
    virtual preflightCheck(){} // <- this I'd love to make pure, currently just warns
                               // currently it just warns about missing implementation
                               // as it should never be visible from a derived class
                               // and this class is purely a base

};

目前我没有任何问题继承和扩展它有几种方法等等,但我希望表达和强制实现在其他子(每个子不同)的方法只能是虚拟和非纯(我提供虚拟实现带有警告,如果它们被调用,指向未实现的派生),因此不能确保未来的用户知道他必须实现它们而不查看源中的注释或doco丢失某处或在日志中捕获我的警告,更不用说我没有编译时间保护我自己的愚蠢。

我有没有办法让其他方法的实现显然是强制性的,而不是纯虚拟的? 或者某种方式来重新具体化类的部分,所以尽管静态创建者需要新的虚拟,但是虚拟的虚拟会被特别接受?

GCC 4.1.2,所以我也从许多C ++ 11细节(覆盖等)中切断,但为了我自己的教育,我很乐意接受可能是前瞻性的答案。我不会被锁定在jurassic API和编译器中。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您是否需要能够拥有myCommand的容器?如果不这样做,您可以通过将myCommand放入模板类中来获得与您正在寻找的行为类似的行为,其中模板参数将提供通常在虚函数。

template <typename PreflightChecker>
class myCommand: public MPxCommand{
public:
    myCommand(){};
    virtual ~myCommand(){};

    static void *creator(){ return new myCommand;} // <-- prevents pure virtual

    static MSyntax syntax // M* are provided API objects/types

    virtual MStatus doIt(const MArgList&);
    virtual MStatus redoIt();
    virtual MStatus undoIt();
protected:
    boilerPlate();
    virtual preflightCheck()
    {
        PreflightChecker::check();
    }
};

class MyPreflightChecker
{
public:
    static void check()
    {
        ...
    }
};

vector<MPxCommand> v;
myCommand<MyPreflightChecker> command;
v.push_back(command);

答案 1 :(得分:0)

你可以这样做:

class A {


  virtual void method() = 0 {
    // now the derived classes HAS to implement it
    // and you still get an implementation

  }

};
相关问题