[xsd-users] Polymorphism, am I doing something wrong?

Boris Kolpackov boris at codesynthesis.com
Mon May 5 13:00:51 EDT 2014


Hi Rob,

Rob Ursem <Rob.Ursem at cmgl.ca> writes:

> We can see that when we request "points()" from a cs_pointGeometryPatch
> class, we get a reference to points_type, which is the abstract datatype.
> I see that when I parse the given xml with the schema, the parser
> instantiates an instance of "cs_arrayOfExplicitPoints3d" but due to
> the fact that the accessor for points is a reference and not a pointer,
> there is no way for me to typecast the abstract type to a derived type.

You can always get a pointer from a reference with the '&' operator:

cs_pointGeometryPatch x = ... // parse

cs_abstractArrayOfPoints3d& ref = x.points ();
cs_abstractArrayOfPoints3d* ptr = &ref;

In your case, you can discover the concrete type like this:

if (cs_arrayOfExplicitPoints3d* x = 
      dynamic_cast<cs_arrayOfExplicitPoints3d*> (ptr))
{
   // It is cs_arrayOfExplicitPoints3d, 'x' points to the instance.
}
else
{
  // Something else.
}

Boris



More information about the xsd-users mailing list