No, you canβt.
What you want, apparently, is Factory (or perhaps Abstract Factory ).
In C ++, you install the Factory class and register collectors.
class FooFactory { public: typedef std::function<Foo*()> Builder;
You can create a singleton of this factory to register derived classes during library loading, which is convenient for the plugin architecture:
FooFactory& GetFooFactory() { static FooFactory F; return F; }
And you can prepare a convenient constructor:
template <typename Derived> Foo* fooBuilder() { return new Derived(); }
Then people should register their derived classes in the factory:
static const bool registeredBar = GetFooFactory().Register("Bar", fooBuilder<Bar>);
Note: It is far from necessary that Factory should be a single, although it is not so vicious because it remains constant after the library is finished loading.
Note. For the correct plugin architecture, you need to use RAII (instead of bool) to handle deregistration when unloading the library. This is much less common.
source share