SGEngine's primary experimental approach is that of everything being a dynamic loadable module.
For the most part, DLLs tend to use C due to C++'s name mangling shenanigans making it hard to link against.
However, C doesn't provide the const safety, nor the structure, which C++ affords - nor the extensions given via STL... so how would we work around this?
Well, we can create our C++ classes as normal, and provide two C-wrappers; one to create a new instance of our class, and another to destroy it.
It's important that you expose a create and a destroy wrapper within the Library and that it creates and destroys any instance created. Otherwise, all sorts of madness can ensue due to newing and deleting memory across DLL boundaries which you shouldn't really have access to.
class SimpleClass
{
public:
SimpleClass();
~SimpleClass();
void simpleFunction(const std::string& myString);
};
extern "C" SimpleClass* create() {
return new SimpleClass;
}
extern "C" void destroy(SimpleClass* pClass) {
delete pClass;
}
If the above class is then pulled in via a library, loaded via whichever your OS supports ( LoadLibrary/dlopen ) you would then pull out the function pointers to the create and destroy functions we've externed ( and are therefore not subject to the C++ name mangling. )
An instance of the class can then be created via calling the create wrapper we've just linked in.
Once we're finished with it, we pass the Class pointer back to the destroy function, and it gets cleaned up.
This sounds like a mad way of doing things, but it's essentially a manual method of calling the Constructor and Destructor, as there's nothing saying your create/destroy functions cannot take parameters to pass through. It just requires a bit of resource management so you don't create or destroy too many instances - but then that should be a given for creating any pointers.
SGEngine does go a step further in that all it's library modules are derived from SGELIB.
Therefore, virtual methods and so on are not a problem assuming you have the above wrapper for create and destroy; as destroying a pointer of the wrong type, is a very bad thing indeed!