Внедрение класса в деривацию - PullRequest
0 голосов
/ 07 апреля 2020

Я борюсь с куском кода, и на данный момент я задаюсь вопросом, возможно ли то, чего я пытаюсь достичь.

Ниже приведен код различных библиотек DLL и фактический кусок кода, который вызывает у меня головную боль

Первая DLL:

// DLL_A
// This DLL is the main SDK which gives me the capability to create and manage extensions
class IViewportExtensionBase
{
public:
    virtual ~IViewportExtensionBase();
    ...
};

template<typename ExtensionType>
class IViewportExtension : public IViewportExtensionBase
{
protected:
    IViewportExtension()
        : IViewportExtensionBase(ExtensionType::_GetName())
    { }
};

class ExtensionsManager
{
public:
    static IViewportExtensionBase* GetExtension(const std::string& extensionName)
    {
        return _extensions[extensionName];
    }

private:
    std::unordered_map<std::string, IViewportExtensionBase*> _extensions;
};

Вторая DLL:

// DLL_B
// This is another library of the SDK which links DLL_A to add a custom capabilities to the extensions
class ViewportExtensionAdditions
{
public:
    virtual ~ViewportExtensionAdditions()
    { }

    virtual void CustomAddition()
    { }
};

template<typename ExtensionType>
class ICustomViewportExtension : public IViewportExtension<ExtensionType>, public ViewportExtensionAdditions
{ };

Расширение DLL

// DLL_EXTENSION
// This DLL contains the actual extension which implements the interface defined by DLL_B 8of course this links DLL_B too)
class ViewportExtension_A : public ICustomViewportExtension<ViewportExtension_A>
{
public:
    void CustomAddition() override;
    ...
}

Актуальная программа выдает мне ошибку:

// This is a piece of code of the final program which loads the extensions and then tries to call the custom capabilities added by DLL_B
// This program knows about the headers of both DLL_A and DLL_B and links them, but it doesn't know about the headers of DLL_EXTENSION which 
// is dynamically loaded by the extensions manager
auto extension = (ViewportExtensionAdditions*)ExtensionsManager::GetExtension("ViewportExtension_A");
if (extension != nullptr)
{
    // This call doesn't work, meaning that is never calls the overridden method in ViewportExtension_A
    // but calls some other method which looks like the virtual table is somewhat off
    extension->CustomAddition();
}

Я боюсь, что для извлечения одного класса вставим другой (что я делаю в DLL_B), а затем приведем к этому другому классу (что я делаю в программе acqual) ) невозможно.

Если на самом деле оказывается, что это невозможно, каким может быть способ достичь того, что я пытаюсь сделать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...