I don't know if I understand your problem correctly, so let me restate it in my own words...
Problem: Given classes B
and D
, determine if D
is a subclass of B
(or vice-versa?)
Solution: Use some template magic! Okay, seriously you need to take a look at LOKI, an excellent template meta-programming library produced by the fabled C++ author Andrei Alexandrescu.
More specifically, download LOKI and include header TypeManip.h
from it in your source code then use the SuperSubclass
class template as follows:
if(SuperSubClass<B,D>::value)
{
...
}
According to documentation, SuperSubClass<B,D>::value
will be true if B
is a public base of D
, or if B
and D
are aliases of the same type.
i.e. either D
is a subclass of B
or D
is the same as B
.
I hope this helps.
edit:
Please note the evaluation of SuperSubClass<B,D>::value
happens at compile time unlike some methods which use dynamic_cast
, hence there is no penalty for using this system at runtime.