Alf P. Steinbach wrote:
* de**********@gmail.com:
>Ive got two classes in the same class hierarchy, and I want to find
their common ancestor. Any idea how to do this?
class A {};
class B : public A {};
class C : public B {};
class D : public B {};
I have pointers to C and D, I want pointers to B.
See the reply by Ian Collins else-thread.
If your enquiry is of a more academic, theoretical nature:
You can check whether the classes /have/ a common ancestor by using
static_cast, possibly combined with SFINAE.
Since it sounded like a nice template exercise, I just tried to do that,
i.e., I tried to whip up a template
template < typename A, typename B >
class have_common_base {
// some SFINAE magic
public:
static bool const value =
// some more magic
};
so that have_common_base<A,B>::value would be true if and only if A and B
are derived from some common base class.
I failed, and your hint didn't quite cut it for me :-(
How would you do it?
[snip]
Best
Kai-Uwe Bux