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
Forum Updated to NodeBB v4.3 + New Features

Const pointer Map and Hash issue

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
2 Posts 2 Posters 225 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 17 Jan 2024, 08:57 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.

    C 1 Reply Last reply 17 Jan 2024, 09:06
    0
    • A astoffregen
      17 Jan 2024, 08:57

      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.

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Jan 2024, 09:06 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

      1/2

      17 Jan 2024, 08:57

      • Login

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