I am making XmlReader and still don't know well where to use const modifier. With this code:
const QMap<const QString, const QString> XmlElement::readDomAttributes(
const QDomElement& domElement) const
{
QMap<const QString, const QString> attributes;
const QDomNamedNodeMap& domAttributes = domElement.attributes();
for (size_t size = domAttributes.size(), i = 0; i < size; ++i)
{
const QDomAttr& domAttribute = domAttributes.item(i).toAttr();
attributes.insert(domAttribute.name(), domAttribute.value());
}
return attributes;
}
I have this error:
/src/xml-element.cpp:21: error: passing ‘const QString’ as ‘this’ argument discards qualifiers [-fpermissive]
In file included from /usr/include/qt/QtCore/QMap:1,
from ../../include/xml-element.h:4,
from ../../src/xml-element.cpp:1:
/usr/include/qt/QtCore/qmap.h: In instantiation of ‘QMap<K, V>::iterator QMap<K, V>::insert(const Key&, const T&) [with Key = const QString; T = const QString]’:
../../src/xml-element.cpp:21:26: required from here
/usr/include/qt/QtCore/qmap.h:730:25: error: passing ‘const QString’ as ‘this’ argument discards qualifiers [-fpermissive]
730 | lastNode->value = avalue;
| ~~~~~~~~~~~~~~~~^~~~~~~~
Will be glad to hear reason of this error and maybe someone can give materials where I could know when I shouldn't use const and reference, because now I am just placing them almost everywhere where I can
I already know that QVector can't contain const objects, but don't sure about QMap