lundi 6 mars 2017

C++ using namespaces for versioning can cause inheritance problems with overrides

We are trying to namespace the versions of our API with namespaces, although we figured that we will be getting some problems with virtual functions :

namespace v1 {
    class someParam {
    public:
        someParam() {};
        virtual ~someParam() {};
    };

    class someClass {
    public:
        someClass() {};
        virtual ~someClass() {};
        virtual bool doSomething(someParam a);
    };


    bool someClass::doSomething(someParam a)
    {
        return true;
    }
}

namespace v2 {

    class someParam : public v1::someParam {
    public:
        bool doParamStuff();
    };
    bool someParam::doParamStuff()
    {
        return true;
    }
}

// Type Aliasing for v2 API
using someClass = v1::someClass;
using someParam = v2::someParam;

// SOME OTHER PROGRAM
class plugin : public someClass
{
public:
    plugin() {};
    virtual ~plugin() {};
    bool doSomething(someParam a) override;

private:
        using v1::someClass::doSomething;
};

In this specific case, we are creating extension of existing classes to allow binary compatibility. Although, we get a compilation error for plugin::doSomething because of the override keyword as it is not overriding someClass::doSomething because:

plugin::doSomething(v2::someParam) vs someClass::doSomething(v1::someParam).

Is there any way to fix up the plugin without explicitely using v1 for someParam in plugin class ? Ideally, nothing should be done on the plugin side, and without having to create v2::someClass

Aucun commentaire:

Enregistrer un commentaire