Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Make shift list to modify pointed to objects
Qt 6.11 is out! See what's new in the release blog

Make shift list to modify pointed to objects

Scheduled Pinned Locked Moved Solved C++ Gurus
15 Posts 5 Posters 3.9k Views 3 Watching
  • 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.
  • Chris KawaC Chris Kawa

    @fcarney Hah, right, I took too fast of a glance. Well, there's your answer. It doesn't help readability ;)

    fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by fcarney
    #6

    @Chris-Kawa I changed the code to be explicit ** in my code. auto is too confusing.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    1
    • fcarneyF fcarney

      I originally did the code this way:

          int* ptr1 = new int;
          int* ptr2 = new int;
      
          for(auto ptr: {ptr1, ptr2}){
              if(ptr){
                  // call something on int*
                  delete ptr;
                  ptr = nullptr;
              }
          }
      

      But this modifies the copy of the pointer in the structure. Not the original pointer. This is a bug to write it this way.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      Work on the reference:

      @fcarney said in Make shift list to modify pointed to objects:

      for(auto &ptr: {ptr1, ptr2}){
      

      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
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #8

        @Christian-Ehrlicher said in Make shift list to modify pointed to objects:

        Work on the reference:

        That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.

        C++ is a perfectly valid school of magic.

        Chris KawaC J.HilkJ 2 Replies Last reply
        0
        • fcarneyF fcarney

          @Christian-Ehrlicher said in Make shift list to modify pointed to objects:

          Work on the reference:

          That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @fcarney That's 0:2 in the game of readability vs this code :)

          1 Reply Last reply
          0
          • fcarneyF fcarney

            @Christian-Ehrlicher said in Make shift list to modify pointed to objects:

            Work on the reference:

            That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #10

            @fcarney you know this is also an option:

            int* ptr1 = new int;
                int* ptr2 = new int;
            
            auto doStuffWith = [](int *ptr){
             if(ptr){
                        // call something on int*
                        delete ptr;
                        ptr = nullptr;
                    }
            };
              doStuffWith(ptr1);
             doStuffWith(ptr2);
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            fcarneyF 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @fcarney you know this is also an option:

              int* ptr1 = new int;
                  int* ptr2 = new int;
              
              auto doStuffWith = [](int *ptr){
               if(ptr){
                          // call something on int*
                          delete ptr;
                          ptr = nullptr;
                      }
              };
                doStuffWith(ptr1);
               doStuffWith(ptr2);
              
              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #11

              @J-Hilk said in Make shift list to modify pointed to objects:

              auto doStuffWith = [](int *ptr){

              This is still a copy of the pointer. It will work if I use a reference though:

              auto doStuffWith = [](int* &ptr){
              

              Long run though, I should probably start using smart pointers:

                  std::shared_ptr<int> ptra(new int);
                  std::shared_ptr<int> ptrb(new int);
              
                  for(auto ptr: { &ptra, &ptrb }){
                      // do stuff
                      ptr->reset();
                  }
                  Q_ASSERT(ptra == nullptr);
              
                  // for qobject
                  QPointer<QObject> ptrc = new QObject();
                  QPointer<QObject> ptrd = new QObject();
              
                  for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh
                      // do stuff
                      delete ptr;
                  }
                  Q_ASSERT(ptrc == nullptr);
              

              C++ is a perfectly valid school of magic.

              Christian EhrlicherC 1 Reply Last reply
              0
              • fcarneyF fcarney

                @J-Hilk said in Make shift list to modify pointed to objects:

                auto doStuffWith = [](int *ptr){

                This is still a copy of the pointer. It will work if I use a reference though:

                auto doStuffWith = [](int* &ptr){
                

                Long run though, I should probably start using smart pointers:

                    std::shared_ptr<int> ptra(new int);
                    std::shared_ptr<int> ptrb(new int);
                
                    for(auto ptr: { &ptra, &ptrb }){
                        // do stuff
                        ptr->reset();
                    }
                    Q_ASSERT(ptra == nullptr);
                
                    // for qobject
                    QPointer<QObject> ptrc = new QObject();
                    QPointer<QObject> ptrd = new QObject();
                
                    for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh
                        // do stuff
                        delete ptr;
                    }
                    Q_ASSERT(ptrc == nullptr);
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @fcarney said in Make shift list to modify pointed to objects:

                for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh

                Nothing is copied here...

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

                fcarneyF 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @fcarney said in Make shift list to modify pointed to objects:

                  for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh

                  Nothing is copied here...

                  fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #13

                  @Christian-Ehrlicher

                  { ptrc, ptrd }
                  

                  That was a crux of the original problem. This structure or whatever it is, has a copy of ptrc and ptrd objects.

                  When the objects were just pointers it was copies of the pointers. That is why setting to null only worked on the copy itself.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #14

                    Oh interesting. The braces with objects create an std::initializer_list. That is why it works as a container with range for loops.

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #15

                      I think this is an example when you should use the STL containers. I frequently create containers of pointers of objects (handles) and iterate over them, but unless the container contains smart pointers you must remember to explicitly delete the objects.

                      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                      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