macros - Is it possible in C++ to loop over all subclasses of an Abstract class? -


I have an abstract class in C ++ with many subclasses.

Is it possible to do something like macros or template metaprogramming something like this:

  Base suburban of the base: mymap [subclass :: SOME_CONSTANT] = new subclass ();    

No, you can not.

What you want, apparently, is a factory (or perhaps abstract factory ).

In C ++, you set up a factory class and register builders.

  class FooFactory {public: typed characters  second) (); } Private: std :: map & lt; Std :: string, builder & gt; _Map; };   

In order to register derivative classes during the library load, you can create a singleton of this factory, which is easy for plug-in architecture:

  FooFactory & amp; GetFooFactory () {Fixed FooFactory F; Refund F; }   

And you can prepare an easy builder:

  template & lt; Typename derived & gt; Foo * Füblilder () {New result (;); }   

Then people are expected to register their derivative classes in the factory:

  Static Const. Bool registered times = GetFooFactory (). Register ("bar" fooBuilder & lt; bar & gt;); Note: It is not necessarily far away that the factory should be singleton, although it is not as bad as it once loads the load of libraries once it is finished.  

Note: For a proper plugin architecture, you have to use RAII (instead of the bowl) to handle the unknowment in the library unload. This is very rare.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -