Connecting Tech Pros Worldwide Forums | Help | Site Map

Detecting virtual inheritance

Imre
Guest
 
Posts: n/a
#1: Mar 27 '06

Hi

I'd like to create a special cast template function that can be used to
cast a base pointer to a derived one, and uses dynamic_cast if virtual
inheritance is involved, and static_cast otherwise. Unfortunately I
don't know how to properly detect VI.
I've seen a trick for detecting VI, that goes something like:

template <class B, class D>
struct IsVirtualBaseAndDerived
{
class Helper1: public D, public virtual B {};
class Helper2: public D {};
public:
enum { value = (sizeof(Helper1) == sizeof(Helper2)) };
};

This works well, but can't be used to detect if there are additional
inheritance levels between B and D, and VI is used somewhere in the
middle. For example:

class A {};
class B: public A {}; // not virtual
class C: public virtual B {}; // virtual

A is not really a VB of C here, but still, if we are to do an A* -> C*
cast, we should use dynamic_cast due to the VI between B and C.

So, my question is: how could I create some
IsVirtualInheritanceInvolved<B, D> template metafunction?

Thanks,

Imre


Gernot Frisch
Guest
 
Posts: n/a
#2: Mar 27 '06

re: Detecting virtual inheritance


[color=blue]
> So, my question is: how could I create some
> IsVirtualInheritanceInvolved<B, D> template metafunction?[/color]

If I'm not totally wrong, you must provide a function like:
std::vector<std::string> InheritanceInformation();

to all of your classes.
Maybe someone's got a brilliant idea. This one propably sux a lot.


Closed Thread