Assume we have the following, given code:
class T; // with T::~T is virtual
class S;
class E {
void foo() {
/* ... */
S s;
T* t = new T(s);
/* ... */
delete t;
/* ... */
}
};
We cannot change E.
Assume now that we have:
class S;
class A {
class T : public ::T {
T(S);
}
};
Is there any way (in C++03!) we can force E to instantiate A::T instead of ::T in E::foo, without changing the implementation of E::foo or E in general?
Note: If really necessary, it would be acceptable to make E inherit from something we specify (e.g. A), but I would prefer not to.
A general note: Yes this smells like a design flaw, but I’m making the concious decision to have a very small part of integration code that is slightly hacked, so that all other (larger!) parts can be beautifully independent and well-designed.
Two answers apply – admittedly both treads the borders of your question:
1. Template it
Now, technically, this did not alter the implementation of E::foo… (if you will, it just defined a more generic one).
2. Evil Macros
The really -dumb- method that will work even in some of the most adverse of circumstances: