Menu

TiXMLHandle and const-ness errors

Developer
2006-05-15
2013-05-20
  • Koen Tanghe

    Koen Tanghe - 2006-05-15

    Hi,

    if I want to read from a node being to supplied one of y functions using a "const TiXmlNode*", and I want to use a TiXmlHandle, I receive errors like:

    error C2664: 'TiXmlHandle::TiXmlHandle(TiXmlNode *)' : cannot convert parameter 1 from 'const TiXmlNode *' to 'TiXmlNode *'
            Conversion loses qualifiers

    which makes sense of course, since I received a const TiXmlNode* variable...

    Is it somehow possible to add a TiXmlHanle(const TiXmlNode*) constructor to the TiXmlHandle class in the next release? Or might that have more profound implications?

    I know I could just do this:
    void MyReadOnlyFunction(const TiXmlNode* inNode)
    {
    const TiXmlHandle handle((TiXmlNode*)inNode);
    ...
    }
    but that's a Bad Thing (TM) to do...

    Koen

     
    • mck9

      mck9 - 2006-05-21

      A TiXmlHandle is just a thin wrapper around a pointer to a <strong>non-const</strong> TiXmlNode.  If you construct a TiXmlHandle from a pointer to a const node, you will have to cast away the constness.  If the constructor you're asking for were available, it would have to do the cast for you, but the cast would still be there.

      Once you had your TiXmlHandle, you could then use TiXmlHandle::Node() to get the underlying pointer -- to a non-const node.  Oops.  Constness is broken.

      I suggest that you clone TiXmlNode into a similar class TiXmlConstHandle, which would store a pointer to a const node.  The member function Node() (and several others like it) would return a pointer to a const node, so as to preserve constness.

       
      • mck9

        mck9 - 2006-05-21

        Oops, I forgot that HTML wouldn't work.  Please ignore the <strong> tag.

         

Log in to post a comment.