1

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

  • It seems the problem is that the object of the value type QMap is declared with the qualifier const but in the method insert there is used the assignment lastNode->value = avalue; that may not be applied to a constant object. It seems you need to use QMap – Vlad from Moscow Jul 04 '23 at 12:17
  • 2
    *'I already know that QVector can't contain const objects,'* This is simply untrue. A QVector cannot contain const objects **if you ever want to modify those objects**. If you don't then it's fine for a QVector to contain const objects. Exactly the same applies to a QMap. If seems you are looking for some generic rules (like QVector is never const) instead of thinking about what const actually **means**. Also, if you don't understand I would advise making nothing const instead of everything const. – john Jul 04 '23 at 12:57
  • 2
    `QMap::insert` looks for the existing entry with the given key and, if found, replaces the old value with the new value. This replacement is done via assignment. For that reason, `insert` won't work on a map whose value type is const-qualified (since, again, that value may need to be modified). – Igor Tandetnik Jul 04 '23 at 15:29
  • Most of the `const`s in this code should to removed. There is no reason to return a `const` object by value. There is no reason for the `QMap` to hold `const` objects in its key and value. There is no need to store `const` references to temporary objects returned by value. The only `const`s you really need are on `readDomAttributes()` itself and its `domElement` parameter, the rest of the `const`s can go away – Remy Lebeau Jul 04 '23 at 18:20
  • Thanks to everyone) From that time, I worked a little on my code style, now i write less unwanted and unclear construction and everything works – Николай Авилкин Jul 09 '23 at 11:39

0 Answers0