I have the following classes:
class base{};
class derived1:public base{};
class derived2:public base{};
I also have an implemented container pool
to which I have no editing access. In each of the 3 .h
files, I define the following pool respectively:
typedef pool<base> basesPool;
typedef pool<base> derived1Pool;
typedef pool<base> derived2Pool;
At some point in the flow, I am getting an object out of the pool, and assigning it to a general pointer of type base
with respect to a flag
:
derived1Pool d1pool;
derived1Pool d2pool;
base* element;
if(flag){
d1pool = new derived1Pool();
element = d1pool ->alloc();
}
else{
d2pool = new derived2Pool();
element = d2pool ->alloc();
}
Later on, I am trying to clean using the following code:
if(flag){
d1pool->free(element);
}
else{
d2pool->free(element);
}
I am getting an error due to a covariance problem:
error: no matching function for call to 'pool<derived1Pool>::free(base*&)'
An easy solution would be to define two pointers. I am trying to avoid this. Is there an elegant option to convert from the base pointer to the derived pointer? I have seen this post and tried all variations of dynamic_casting
I could think of to no avail.