Search code examples
c++pluginsinterfacenulldereference

Skip Unused Virtual Function


I have main executable and two functions that are dereferenced to DLL.

class CPluginInterface
{
public:
    virtual void A(void) = 0;
    virtual void B(void) = 0;
};

I created DLL like this

//Main.h
#include "Function.h"

class CForward : public CPluginInterface
{
public:
    //void A(void);
    void B(void);
};

//Main.cpp
#include "Main.h"

/*void CForward::A(void)
{
    //A Function is commented because it is not used
}*/

void CForward::B(void)
{
    //Do something here
}

extern "C"
{
    // Plugin factory function
    //void __declspec(dllexport) __cdecl A(void) { }
    void __declspec(dllexport) __cdecl B(void) { }
}

However the program is crashed because A(void) doesn't exist when the main executable dereferencing it. How to skip A(void)?

If I create the DLL like this, it works fine.

//Main.h
#include "Function.h"

class CForward : public CPluginInterface
{
public:
    void A(void);
    void B(void);
};

//Main.cpp
#include "Main.h"

void CForward::A(void)
{
    //Do something here
}

void CForward::B(void)
{
    //Do something here
}

extern "C"
{
    // Plugin factory function
    void __declspec(dllexport) __cdecl A(void) { }
    void __declspec(dllexport) __cdecl B(void) { }
}

NB: I create Plugin Interface.


Solution

  • The =0 suffix on your interface's virtual functions indicate that they are pure virtual and that you are required to override them when inheriting from the base class. In your first example CForward is an abstract class because you have not overridden A, thus you cannot create an instance of CForward.

    https://stackoverflow.com/a/2652223