Here, the overload chosen depends on the type of node
, not the type of aNode
.
const
at the end of the member function declaration indicates that the function may be called on a const instance of the object; a member function lacking const
there cannot be. When both const
and non-const
overloads are present, overload resolution prefers the const
version when applicable.
Apparently, node
is of type const TiXmlNode*
, so FirstChild() const
overload is chosen, and the return value is of type const TiXmlNode*
. This cannot be converted to TiXmlNode*
.
Here's a textbook example. std::vector<T>
provides two overloads of operator[]
, looking like this (greatly simplified):
template <typename T>
class vector {
T* storage_;
public:
T& operator[](size_t index) { return storage_[index]; }
const T& operator[](size_t index) const { return storage_[index]; }
};
With this, you could write:
vector<int> v;
v[0] = 42; // OK: calls int& operator[](size_t)
const vector<int> cv;
cv[0] = 42; // Error: calls const int& operator[](size_t) const
As expected, const vector
can't be modified, while non-const one can be.