Re: Dynamically choosing what to "new"
I've run into a strange problem, but one that seems like it might be
It is common.
I don't know of any other generic factory implementations but the one in
Austria C++ will allow you to register a number of different factories
(named Dog, Cat etc) and simply request construction on an appropriate one.
int main( int argc, char ** argv )
if ( argc == 2 )
Animal * animal = at::FactoryRegister<
>::Get().Create( argv )();
// the Create method takes a few other arguments
// one that tells it to throw on failure
if ( ! animal )
// this stuff below can be in any source file or even DLL/DSO
struct Dog : Animal
// AT_MakeFactory0P is a macro that "registers" a factory
// for Dog under the key "Dog" over it's interface Animal
// using a key type of at::DKy - you could use std::string
// here if you wanted to.
AT_MakeFactory0P( "Dog", Dog, Animal, at::DKy );
struct Cat : Animal
AT_MakeFactory0P( "Dog", Cat, Animal, at::DKy );
Careful that you make sure the factory object files are linked in.
That's the usual source of frustration using this. There are a couple
of tools which help you do that but it's the only issue.