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

Rob Ursem Rob.Ursem at cmgl.ca
Mon May 5 13:54:56 EDT 2014


Boris,

I thought that's what I had replied to Wesley but I added a "DUH" to it as it was a headslap moment.
But then again, I sent that email from the airport so it might not have gone through.

It all works nicely now. Thanks for the help!

Rob


-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: Monday, May 05, 2014 11:01 AM
To: Rob Ursem
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] Polymorphism, am I doing something wrong?

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