If I have two classes A and B defined as:
class A {
map<int, int> mMap;
vector<int> mVec;
void method() {
// do something with mMap and mVec
}
}
class B {
map<int, double> mMap; // just an example of a map with a different signature
...
}
It is possible somehow to use everything A has in B but with the new map defined in B and without rewriting method ?
This is a simplified example of what I am trying to do: refactoring a big class with a new map.
If you want to use
instead of
with the same code, to replace
class Awithclass B, then there probably exists a strong correlation between the typesXandY.In this case, if you cannot use templates on
class A, a possible workaround may be to define implicit type conversion operators for bothYandXtoXandYrespectively.By doing so, you don’t need to change the code of
methodas the insertion or access operations on the map will automatically call the implicit type conversion operators.Warning note: the abuse of implicit conversion operators weakens the type safety checkings of the compiler that you may expect in other parts of the code (e.g.: wrong
Yparameter passed tof(X &x)).