[xsd-users] constructor

Boris Kolpackov boris at codesynthesis.com
Tue Oct 31 13:49:46 EST 2006


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



-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20061031/85213ccf/attachment.pgp


More information about the xsd-users mailing list