Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Const pointer Map and Hash issue
Qt 6.11 is out! See what's new in the release blog

Const pointer Map and Hash issue

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
2 Posts 2 Posters 369 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astoffregen
    wrote on last edited by
    #1

    I have a problem understanding QMap and QHash. I have already read that QMap provides a copy of the content and not a pointer / reference.

    QMap<int, TextItem> items;
    items.insert(0, TextItem());
    items.insert(1, TextItem());
    

    Do I still get a pointer to the content that I can use with this statement - it gives no compiler error?

    TextItem a = items.value(0);
    TextItem* b = &a;
    

    This not

    TextItem* c = &items.value(0);
    

    Error is - a value from type const TextItem* could not use to initialize a entity of type TextItem*.

    It works with QList, it must be a special feature of QMap.

    Christian EhrlicherC 1 Reply Last reply
    0
    • A astoffregen

      I have a problem understanding QMap and QHash. I have already read that QMap provides a copy of the content and not a pointer / reference.

      QMap<int, TextItem> items;
      items.insert(0, TextItem());
      items.insert(1, TextItem());
      

      Do I still get a pointer to the content that I can use with this statement - it gives no compiler error?

      TextItem a = items.value(0);
      TextItem* b = &a;
      

      This not

      TextItem* c = &items.value(0);
      

      Error is - a value from type const TextItem* could not use to initialize a entity of type TextItem*.

      It works with QList, it must be a special feature of QMap.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @astoffregen said in Const pointer Map and Hash issue:

      TextItem* c = &items.value(0);

      Apart from a possible compiler error this will result in a dangling pointer as the returned object is destroyed after the semicolon.

      Wrt the error I would guess you use Qt5 where QMap::value() returns a const object as you can see in the documentation: https://doc.qt.io/qt-5/qmap.html#value

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved