[xsd-users] polymorphism through xs:choice

Boris Kolpackov boris at codesynthesis.com
Tue Jun 11 08:52:28 EDT 2013


Hi Harg,

Harg Tholan <hargtholan at gmail.com> writes:

> The question is, how can I retrieve the actual type of the referenced
> element? I need to know the actual type because I need to "dynamic cast"
> to it, but I can not know beforehand.

You cannot "retrieve" a C++ type at runtime and dynamic_cast to it. C++
is a compiled, statically-type language. What you can do is discover
whether an instance is of one of the types from a predefined (at compile
time) set. For example:

Base& x = ... // Gen element from the choice. 

if (DerivedA* a = dynamic_cast<DerivedA> (&x))
{
  // It is DerivedA.
}
else if (DerivedB* b = dynamic_cast<DerivedB> (&x))
{
  // It is DerivedB.
}

Other things that you could do are define a virtual function in Base
and then implement it in DerivedX (using type customization) or call
typeid() on the instance to get, for example, the type name.

Boris



More information about the xsd-users mailing list