Coming from this thread I implemented a similar system in c++ to the chosen solution there.
My problem now is that it is stated there by the user Daniel James that this solution might not work with every compiler (I'm using gcc currently) and is not defined in the c++ standard.
Suppose I have an abstract base-class for the interface and a factory-class as a singleton that stores pointers to a function that constructs the specific classes derived from that interface.
then I have a helper class that looks roughly like this:
base.hpp
...
class implRegistrator {
public:
implRegistrator(constructPointer) {
factory::registerImpl(constructPointer);
};
}
And an implementation that (through a macro) creates an object of this class to register itself:
impl1.cpp
...
implRegistrator* impl1 = new implRegistrator(getConstructPointer());
How compatible to the C++ standard is this solution? Is it safe to assume that the class instantiation ind impl1.cpp will even happen, since nothing from the main program will actually explicitly call it at compile-time?
Thanks in advance for any answers.
.so
vs..a
scenarios..dll
or a.so
is not a library;.so
stands for "shared object", and both behave as object files, not libraries.)constructPointer
, and what doesgetConstructPointer()
do. There are ways to implement automatic registration code which are standard conformant.