I have the following problem (simplified version):
Interface X;
class Y : public X { ... }
class Z : public X { ... }
class A
{
map<int, X> m;
init() { X bla = new Y or Z; map.insert(...) }
}
I would like to specify either Y or Z at creation time of A, thus getting a different functionality based on type specified. Currently I could “templatize” A, but that would mean EVERY function I define for A will have to have the template specified, even though I need to use it only in one method. Also, I am using g++ 4.2.4, which means I can’t separate my implementation of functions from the header file.
What is usually done in this case is moving the common functionality, which is not templatized, to a base class, and defining a templatized
Athat inherits the base functionality. I.e.:This is a common technique to reduce code bloat. In case that
A_baseneeds to call a method ofA, you can make itvirtual.