Is it possible in C ++ to iterate over all subclasses of an abstract class?

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

Somehow, using macro programs or macros, you can do something like this:

foreach subclass of Base: mymap[subclass::SOME_CONSTANT] = new subclass(); 
+4
source share
2 answers

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; /// returns true if the registration succeeded, false otherwise bool Register(std::string const& key, Builder const& builder) { return map.insert(std::make_pair(key, builder)).second; } /// returns a pointer to a new instance of Foo (or a derived class) /// if the key was found, 0 otherwise Foo* Build(std::string const& key) const { auto it = _map.find(key); if (it == _map.end()) { return 0; } // no such key return (it->second)(); } private: std::map<std::string, Builder> _map; }; 

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.

+7
source

C ++ does not allow iterating over types. Also listing members is not possible. See this:

 enum Color { red, green, blue=5, yellow }; 

Next, the C ++ compiler compiles the compilation units independently. You can visualize that when compiling the base class implementation it may not be known that the class will be inherited sometimes.

0
source

Source: https://habr.com/ru/post/1345439/


All Articles