C++ SAX Question

RJA at dip.co.uk RJA at dip.co.uk
Wed Oct 21 15:14:09 BST 1998

Richard J. Anderson at DI
10/21/98 02:14 PM


I'm currently working on some C++ SAX definitions has was wondering if I
should be using references for strings or not.    Take the following
snippet from the definitions:

class CSAXDocumentHandler
    virtual void startDocument() = 0;
    virtual void endDocument() = 0;
    virtual void startElement( string sName,
                      CSAXAttributeList& Attributes ) = 0;
    virtual void endElement( string sName ) = 0;
    virtual void processingInstruction( string sTarget, string sData ) = 0;
    virtual void characters( char ch[],
                    int start,
                    int length) = 0;
    virtual void setDocumentLocator( CSAXLocator &locator ) = 0;
    virtual void ignorableWhitespace( char ch[],
                          int start,
                          int length) = 0;

    //+ additonal methods not supported by SAX

    virtual void comment( string Comment ) = 0;

Each method that accepts a string parameter is defined to accept it by
value ( eg 'string' )  rather than by reference, ( eg 'string&' ).   Is
this a good or bad idea ?

So far I can think of the following reasons why a reference would be
1. No additional/allocation is required for each element callback.
2.  Passing by value can cause memory allocation issues.  For example, in a
dll, the string would be constructed by the caller of the function on their
heap  (eg.  the  parser),  but  deleted  by  the implemetor of the document
handler  class  whose  heap would probably raise an exception.  ( I'm still
pretty  new  to STL so maybe you can change the allocation mechanism or I'm
completly mistaken :-) ).

I  would  be  interested  in hearing some feedback on this subject, as I an
hoping to publish my C++ SAX definitions in the next week or so.

Kind Regards,

Richard Anderson.

***  This post is of a personal nature and does not in anyway represent the
opinions or developments of data interchange plc

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)

More information about the Xml-dev mailing list