[xsd-users] polymorphism plus sequence equals slicing objects

Boris Kolpackov boris at codesynthesis.com
Sun Sep 9 17:19:58 EDT 2007


Hi Kroizman,

Kroizman Guy <kroiz at hyperroll.com> writes:

> Is it possible to use polymorphism in a sequence?

Yes, it is.


> because a sequence of a base elements is translated to:
> vector<base>
> and not:
> vector<base*>

No, actually it is translated to

vector<smart_ptr<base>>

Where smart_ptr is an implementation-specific smart pointer. When
you do a push_back the sequence implementation uses clone() instead
of copy constructor to make a complete copy of the object. The sequence
also uses iterator adapters so that the result of say *s.begin () is
a reference to base, not smart_ptr<base>.


> and then when (for instance) if we create a derived class from base
> and push it to the vector it gets sliced.

Are you create the derived class yourself or is it generated from
a schema type by XSD? If it is generated by XSD then everything
should work fine. Note also that you need to use polymorphism (the
--generate-polymorphic option) in order for such a sequence to be
properly serialized to/parsed from XML.


Boris




More information about the xsd-users mailing list