I'm an experienced developer in a few compiled OOP languages, particularly Object Pascal and C#. Have 'messed around' with C++ for years but recently started getting more serious about C++ development.
Most of concepts in C++ are quite easy for me to grasp based on my experience with other languages. But one thing that I'm finding quite difficult is the ways in which the const
directive is used and how it behaves in C++.
In particular, at the moment I'm faced with this problem, using the TinyXML Library in Netbeans C++ IDE, on an Ubuntu 12.04 machine and default mingW/G++ tool chain:
I'm calling this function:
TiXmlNode::TiXmlNode* FirstChild()
In the TinyXML source there are two overloaded public versions of this function in class TiXmlNode
:
const TiXmlNode* FirstChild() const { return firstChild; }
TiXmlNode* FirstChild() { return firstChild; }
They are identical except for the const directive. I assumed that the version called would be dependent on how I declared the variable I was loading from the function, for example:
const TiXmlNode* aNode = node->FirstChild();
Would call the const version of the function
TiXmlNode* aNode = node->FirstChild();
Would call the second version, without const.
But when I try to use the second form in my code, I get a compiler error:
error: invalid conversion from ‘const TiXmlNode*’ to ‘TiXmlNode*’ [-fpermissive]
Why is this happening? How do I use the version of the function without const? What am I missing here?
More - where can I find a good summary of the usages of const
directive in C++ 11.