User of my library will ask for an instance of any class via getInstance<T>()
.
Among all classes {T1,T2,T3,...,Tn}
that user asks (appearance of callling getInstance<T>()
) in a certain program, if there is a pair of (i,j)
and Ti
derived from Tj
, I will create only the bigger one, e.g. Ti
.
A more complex situation, assume that user's code code ask me to create :-
- Physic
- Rigid
- Constraint
-
Graphic
and their inheritance is as followed:-
My code will create minimum amount of classes that cover the whole tree.
In this case, the best solution are 3 : Rigid
, Constraint
, Graphic
.
If there are many minimum solutions, any one of them are OK.
Here is what I dream :-
template<class T> T* getInstance(){
//?
}
class B{};
class C{};
class D : public B{};
int main(){
//..... allow to insert some code here ...... (a little less preferable)
B* b=getInstance<B>(); //return D* (implicited cast to B*)
C* c=getInstance<C>(); //return C*
D* d=getInstance<D>(); //return D*
assert( static_cast<D*>(b) == d );
delete d; delete c;
//no memory leak
}
How to do it?
This is closest question : Create "custom typeid" for many types AND cache their sizeof()
I feel that it is possible : I have to cache the type by using some lambda / std::function
, but it is too rough from a concrete idea.
Just in case it is not so obvious - this is not a homework / interview.
Aucun commentaire:
Enregistrer un commentaire