[Fwd: Re: [xsd-users] polymorphism plus sequence equals slicing objects]

Kroizman Guy kroiz at hyperroll.com
Mon Sep 10 03:41:04 EDT 2007


-------- Forwarded Message --------
From: Kroizman Guy <kroiz at hyperroll.com>
To: Boris Kolpackov <boris at codesynthesis.com>
Subject: Re: [xsd-users] polymorphism plus sequence equals slicing
objects
Date: Mon, 10 Sep 2007 10:40:26 +0300

what I am trying to do

auto_ptr<Base> c;

if(condition1())
{
	c = new Derived1();
}
if(condition2())
{
	c = new Derived2();
}

root.item().push_back( c );

can I do that?
because what happen is that the overloaded push_back in sequence is not
called, instead the push_back of vector gets call. and I guess that is
not good.


On Mon, 2007-09-10 at 09:40 +0300, Kroizman Guy wrote:
> Oh I see, my bad, I did not see the sequence_common.
> Thanks.
> 
> On Sun, 2007-09-09 at 23:19 +0200, Boris Kolpackov wrote:
> > 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