[xsd-users] constructor
Schmilinsky, Remsy
Remsy.Schmilinsky at ccra-adrc.gc.ca
Wed Nov 1 14:43:01 EST 2006
Thank you Boris, it worked. I followed your recommendation. By the way,
just a minor correction:
the last line of this block:
// Construct contact in-place.
//
contacts::contact _contact ("123456", "name", "email", "888-888-888");
cr.push_back (_contact);
should read:
cr.contact().push_back (_contact);
Remsy
-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sent: October 31, 2006 1:50 PM
To: Schmilinsky, Remsy
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] constructor
Hi Remsy,
Schmilinsky, Remsy <Remsy.Schmilinsky at ccra-adrc.gc.ca> writes:
> <xsd:complexType name="queryResponse">
> <xsd:sequence>
> <xsd:element name="message" type="xsd:string"/>
> <xsd:element maxOccurs="1" minOccurs="0" name="catalog"
type="cts:catalog"/>
> </xsd:sequence>
> </xsd:complexType>
>
> <!-- root element to hold a queryResponse -->
> <xsd:element name="queryResponse" type="query:queryResponse"/>
>
>
> ...and I don't understand the generated constructors:
>
> queryResponse (const message::type&);
This is the constructor that you would normally use to create an object
from scratch. Its arguments are all required members (attributes and
elements). In your case you have one required element (message) and one
optional (catalog). You can read more about this constructor in Section
2.7, "Mapping for Complex Types" of the C++/Tree Mapping Manual:
http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#2.7
> queryResponse (const ::xercesc::DOMElement&,
> ::xml_schema::flags = 0,
> ::xml_schema::type* = 0);
This is the constructor that is responsible for creating an instance
from
its XML representation. You would normally not use this c-tor directly
though you may need to if you choose to interface with some other code
that exposes its data model as Xerces-C++ DOM (e.g., the Berkeley DB XML
database).
> queryResponse (const queryResponse&,
> ::xml_schema::flags = 0,
> ::xml_schema::type* = 0);
This is a copy constructor.
> My test code is this:
>
> contacts::catalog _catalog;
> contacts::contact _contact("123456", "name", "email", "888-888-888");
> _catalog.contact().push_back(_contact);
>
> // ERROR: inappropiate constructor
> query::queryResponse myQueryResponse = query::queryResponse(
> std::string("message"), _catalog);
To make your code work you will need to change your last line like this:
query::queryResponse myQueryResponse ("message");
myQueryResponse.catalog (_catalog);
In other words you use modifier member functions to set optional
members.
Also note that your code is not optimal since it does quite a bit of
copying which can be avoided. I would rewrite your code like this:
query::queryResponse myQueryResponse ("message");
// Set to an empty container.
//
myQueryResponse.catalog (contacts::catalog ());
// Get a refernce to myQueryResponse's catalog container.
//
contacts::catalog& cr (myQueryResponse.catalog ().get ());
// Construct contact in-place.
//
contacts::contact _contact ("123456", "name", "email", "888-888-888");
cr.push_back (_contact);
The idea is to add items to myQueryResponse's catalog in-place rather
than creating them on the side and then copying to myQueryResponse.
HTH,
-Boris
More information about the xsd-users
mailing list