I need have classes which implement similar things (different types of neural networks), but with somewhat different interfaces.
Doing research, I want to run the program with different neural network in the composing class. I want to be able to substitute and of existing types. It would be nice to handle this by run-time start up parameter, but compile-time placement would work as well.
I see at least 5 approaches with their drawbacks which stops me from using them:
- Implement base class with virtual functions and place pointer to base class into composing class. Main drawbacks: risks of object slicing, hell of cloning functions, assignment and comparison operators for the derived classes, dynamic_cast or similar dirty tricks to identify real object type for apply type-specific functions.
- Use template for neural network type. Drawback: this will require most of code to be moved to templates and, as the result I will have to put lots of implementation code into header files.
- Unify interfaces by adding placeholder functions. Drawback: interfaces will become dirty hell.
- Use C++ preprocessor with #ifdef blocks to separate two versions of code. Drawback: it is dirty copy/paste of blocks of code and with more than 2 classes comes to hell.
- Use adapter pattern which will provide unified interface and hide the details. Drawback: actually, it will be implemented with the pointer to implementation, so will suffer both from (1) and (2), but (2) will be only in adapter interface.
Are there better ways in the modern C++ to implement this? Something tells me that I can build some kind of solution around blocks constructed with std::function, but I am not sure yet.