Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Would be an c++ topic, which is the way to change variables values from references by a QString<List>?
Forum Updated to NodeBB v4.3 + New Features

Would be an c++ topic, which is the way to change variables values from references by a QString<List>?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 487 Views 1 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.
  • RipleyR Offline
    RipleyR Offline
    Ripley
    wrote on last edited by
    #1

    Hi everybody i got have this piece of code. My main target is change the values of the variables to 0 but when it's iterating in foreach function doesn't changes their values. Example:

    QList<int> integerList = {var1, var2, var3};
    
    foreach(int x, integerList)
        x = 0;
    

    And if build it as reference this sets me problems

    QList<int> integerList = {var1, var2, var3};
    
    foreach(int &x, integerList)
        x = 0;
    
    JonBJ 1 Reply Last reply
    0
    • RipleyR Ripley

      Hi everybody i got have this piece of code. My main target is change the values of the variables to 0 but when it's iterating in foreach function doesn't changes their values. Example:

      QList<int> integerList = {var1, var2, var3};
      
      foreach(int x, integerList)
          x = 0;
      

      And if build it as reference this sets me problems

      QList<int> integerList = {var1, var2, var3};
      
      foreach(int &x, integerList)
          x = 0;
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ripley
      I'm not a C++ expert, but it's to do with when it takes copies of the elements and foreach, or something like that. Is it that bad if you write instead:

      for (int i = 0; i < integerList.size(); i++)
          integerList[i] = 0;
      
      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        try the c++ 11 range base loop instead of the foreach macro ?

        QList<int> integerList = {1, 2, 3};
          for (int& x : integerList) {
            x = 0;
          }
        
        

        however, you will only change the ints inside the list, not the original var1-var3
        as they are just copied to the list.

        J.HilkJ 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          try the c++ 11 range base loop instead of the foreach macro ?

          QList<int> integerList = {1, 2, 3};
            for (int& x : integerList) {
              x = 0;
            }
          
          

          however, you will only change the ints inside the list, not the original var1-var3
          as they are just copied to the list.

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

          @mrjj said in Would be an c++ topic, which is the way to change variables values from references by a QString<List>?:

          however, you will only change the ints inside the list, not the original var1-var3
          as they are just copied to the list.

          does the initializer list accept variables ?


          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.

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

            @mrjj said in Would be an c++ topic, which is the way to change variables values from references by a QString<List>?:

            however, you will only change the ints inside the list, not the original var1-var3
            as they are just copied to the list.

            does the initializer list accept variables ?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @j-hilk
            Well yes and no. :)
            it will accept what class accepts but
            by value will result in a copy.

            But you can do funky stuff if you like

            int funky1=100;
              QList<int*> integerList = {&funky1};
              *integerList[0] = 90;
            
              qDebug()<< funky1;
            
            

            output: 90

            however, im not sure its possible with no * or &.
            then you need overloaded = i guess to keep tab on the actual variable.

            RipleyR 1 Reply Last reply
            2
            • mrjjM mrjj

              @j-hilk
              Well yes and no. :)
              it will accept what class accepts but
              by value will result in a copy.

              But you can do funky stuff if you like

              int funky1=100;
                QList<int*> integerList = {&funky1};
                *integerList[0] = 90;
              
                qDebug()<< funky1;
              
              

              output: 90

              however, im not sure its possible with no * or &.
              then you need overloaded = i guess to keep tab on the actual variable.

              RipleyR Offline
              RipleyR Offline
              Ripley
              wrote on last edited by
              #6

              @mrjj Thanks my friend your answer has helped me.

              1 Reply Last reply
              0

              • Login

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