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. Trouble assigning value to boolean in the struct
Forum Updated to NodeBB v4.3 + New Features

Trouble assigning value to boolean in the struct

Scheduled Pinned Locked Moved Solved C++ Gurus
32 Posts 7 Posters 4.5k Views 2 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.
  • artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by
    #21

    Thank you for the explanation - indeed, I saw in docs that value() is const but since it worked for the QString I just... assumed it works anyway. Silly of me.

    So, to make it work as intended I should use [index] notation, like @hskoglund said (I am somehow opposed to using it in the favour of methods).

    Since I already got everyones involved attention - the QVector (in this case) is supposed to hold less than 20 of items, set once and not being manipulated further. Is the choice of QVector over other container types a valid one? I read about container classes and differences between them and after that I rather tend to avoid QList.

    For more information please re-read.

    Kind Regards,
    Artur

    J.HilkJ 1 Reply Last reply
    0
    • artwawA artwaw

      Thank you for the explanation - indeed, I saw in docs that value() is const but since it worked for the QString I just... assumed it works anyway. Silly of me.

      So, to make it work as intended I should use [index] notation, like @hskoglund said (I am somehow opposed to using it in the favour of methods).

      Since I already got everyones involved attention - the QVector (in this case) is supposed to hold less than 20 of items, set once and not being manipulated further. Is the choice of QVector over other container types a valid one? I read about container classes and differences between them and after that I rather tend to avoid QList.

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

      @artwaw said in Trouble assigning value to boolean in the struct:

      after that I rather tend to avoid QList.

      👍 very good.

      If you want to stay in Qt - World, than there are not many other options available.
      A simple c/c++ array could be enough for your case?


      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.

      artwawA 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @artwaw said in Trouble assigning value to boolean in the struct:

        after that I rather tend to avoid QList.

        👍 very good.

        If you want to stay in Qt - World, than there are not many other options available.
        A simple c/c++ array could be enough for your case?

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #23

        @J-Hilk Might be enough, of course. QVector seemed... just convenient.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        0
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #24

          Thank you all, I learned a lot from my mistake. Will pay more attention to docs and implications next time.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #25

            Now that the OP appears to be happy he is solved, I'd like to say that I am not! :) And would ask for further explanation.

            I wrote earlier:

            @artwaw said in Trouble assigning value to boolean in the struct:
            If const is the cause of the error (and I don't doubt it is) for

            current.fields.value(ui->editSectionNum->currentIndex()).permanent=(state>0);`
            

            why is there no error for

            current.fields.value(ui->editSectionNum->currentIndex()).pattern=ui->sectionEdit->text();
            

            ? That was the OP's query. That's what I don't see, it should produce the same error.

            @hskoglund replied

            I suspect it's because the const is only 1 layer deep, i.e. it protects integers and booleans in that struct from being reassigned, but since QStrings are pointers, that const only protects reassignment of the pointer, not what the pointer points to, i.e. QStrings data().

            I know the const "is only one level deep". And I don't understand how he comes up with that "the pointer value will be retained but what it points to will be changed".

            And @Christian-Ehrlicher wrote

            In theory it's a temporary so it should compile.

            I don't understand either of these. The OP's struct has

              QString pattern;
              bool permanent;
            

            I have yet to understand why the bool can be altered but not the QString. (Yes, I know QStrings are stored shared, so what?) They are both members of the struct. If current.fields.value() returns a const then neither of them should be alterable, and if it does not both should be alterable.

            Would somebody care to enlighten me on what exactly is the difference?

            artwawA kshegunovK 2 Replies Last reply
            1
            • JonBJ JonB

              Now that the OP appears to be happy he is solved, I'd like to say that I am not! :) And would ask for further explanation.

              I wrote earlier:

              @artwaw said in Trouble assigning value to boolean in the struct:
              If const is the cause of the error (and I don't doubt it is) for

              current.fields.value(ui->editSectionNum->currentIndex()).permanent=(state>0);`
              

              why is there no error for

              current.fields.value(ui->editSectionNum->currentIndex()).pattern=ui->sectionEdit->text();
              

              ? That was the OP's query. That's what I don't see, it should produce the same error.

              @hskoglund replied

              I suspect it's because the const is only 1 layer deep, i.e. it protects integers and booleans in that struct from being reassigned, but since QStrings are pointers, that const only protects reassignment of the pointer, not what the pointer points to, i.e. QStrings data().

              I know the const "is only one level deep". And I don't understand how he comes up with that "the pointer value will be retained but what it points to will be changed".

              And @Christian-Ehrlicher wrote

              In theory it's a temporary so it should compile.

              I don't understand either of these. The OP's struct has

                QString pattern;
                bool permanent;
              

              I have yet to understand why the bool can be altered but not the QString. (Yes, I know QStrings are stored shared, so what?) They are both members of the struct. If current.fields.value() returns a const then neither of them should be alterable, and if it does not both should be alterable.

              Would somebody care to enlighten me on what exactly is the difference?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #26

              I'd be happy to read further on the details too, if our more experienced colleagues don't mind?

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • JonBJ JonB

                Now that the OP appears to be happy he is solved, I'd like to say that I am not! :) And would ask for further explanation.

                I wrote earlier:

                @artwaw said in Trouble assigning value to boolean in the struct:
                If const is the cause of the error (and I don't doubt it is) for

                current.fields.value(ui->editSectionNum->currentIndex()).permanent=(state>0);`
                

                why is there no error for

                current.fields.value(ui->editSectionNum->currentIndex()).pattern=ui->sectionEdit->text();
                

                ? That was the OP's query. That's what I don't see, it should produce the same error.

                @hskoglund replied

                I suspect it's because the const is only 1 layer deep, i.e. it protects integers and booleans in that struct from being reassigned, but since QStrings are pointers, that const only protects reassignment of the pointer, not what the pointer points to, i.e. QStrings data().

                I know the const "is only one level deep". And I don't understand how he comes up with that "the pointer value will be retained but what it points to will be changed".

                And @Christian-Ehrlicher wrote

                In theory it's a temporary so it should compile.

                I don't understand either of these. The OP's struct has

                  QString pattern;
                  bool permanent;
                

                I have yet to understand why the bool can be altered but not the QString. (Yes, I know QStrings are stored shared, so what?) They are both members of the struct. If current.fields.value() returns a const then neither of them should be alterable, and if it does not both should be alterable.

                Would somebody care to enlighten me on what exactly is the difference?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #27

                @JonB said in Trouble assigning value to boolean in the struct:

                If current.fields.value() returns a const then neither of them should be alterable, and if it does not both should be alterable.

                Look carefully at what I wrote to Henry. The struct's instance returned by QVector::value is not const. It's a temporary (probably the compiler does it as an rvalue), so it's valid to write its members. However the compiler deduces that assigning to the boolean of said temporary is meaningless, thus as @Christian-Ehrlicher eloquently put it - it prevents you from shooting yourself in the foot. When it comes to classes and structs it's more complicated, which we discussed briefly with Christian upstairs.

                Read and abide by the Qt Code of Conduct

                JonBJ 1 Reply Last reply
                4
                • kshegunovK kshegunov

                  @JonB said in Trouble assigning value to boolean in the struct:

                  If current.fields.value() returns a const then neither of them should be alterable, and if it does not both should be alterable.

                  Look carefully at what I wrote to Henry. The struct's instance returned by QVector::value is not const. It's a temporary (probably the compiler does it as an rvalue), so it's valid to write its members. However the compiler deduces that assigning to the boolean of said temporary is meaningless, thus as @Christian-Ehrlicher eloquently put it - it prevents you from shooting yourself in the foot. When it comes to classes and structs it's more complicated, which we discussed briefly with Christian upstairs.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #28

                  @kshegunov said in Trouble assigning value to boolean in the struct:

                  instance returned by QVector::value is not const. It's a temporary

                  Good, got that (was always dubious about whether it was const anyway).

                  However the compiler deduces that assigning to the boolean of said temporary is meaningless

                  Ah ha!! Got it! Sooooooooo... what you are saying is: on the one hand we now have a compiler which gets involved in the meaning of life/teleological purpose, yet on the other hand chooses to blurt out "Expression not assignable" as the diagnostic message when it's non-such. "Meaningless assignment", "Assignment with no side-effect" would have been much better, "non-assignable" means e.g. it's const to me, hence my confusion (and perhaps that of @artwaw).

                  What happened to the days when I wrote code and the compiler knew its place, minded its business and just shut up and compiled?

                  kshegunovK 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @kshegunov said in Trouble assigning value to boolean in the struct:

                    instance returned by QVector::value is not const. It's a temporary

                    Good, got that (was always dubious about whether it was const anyway).

                    However the compiler deduces that assigning to the boolean of said temporary is meaningless

                    Ah ha!! Got it! Sooooooooo... what you are saying is: on the one hand we now have a compiler which gets involved in the meaning of life/teleological purpose, yet on the other hand chooses to blurt out "Expression not assignable" as the diagnostic message when it's non-such. "Meaningless assignment", "Assignment with no side-effect" would have been much better, "non-assignable" means e.g. it's const to me, hence my confusion (and perhaps that of @artwaw).

                    What happened to the days when I wrote code and the compiler knew its place, minded its business and just shut up and compiled?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #29

                    @JonB said in Trouble assigning value to boolean in the struct:

                    What happened to the days when I wrote code and the compiler knew its place, minded its business and just shut up and compiled?

                    They've passed some time ago. The compilers've become smarter than us (developers) so we only can bite the bullet on that one, sorry. I expect soon compilers to be writing compilers, Terminator 2-style ...

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #30

                      @kshegunov @Christian-Ehrlicher I have another question: since value() returns const but copy of QString occurs (re my very unfortunate construction with assignment to QString) - will the value assigned to that QString remain in the QVector? My understanding is that not, after terminating the method copy would be discarded and new value lost. Do I understand correctly?

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      kshegunovK 1 Reply Last reply
                      0
                      • artwawA artwaw

                        @kshegunov @Christian-Ehrlicher I have another question: since value() returns const but copy of QString occurs (re my very unfortunate construction with assignment to QString) - will the value assigned to that QString remain in the QVector? My understanding is that not, after terminating the method copy would be discarded and new value lost. Do I understand correctly?

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #31

                        @artwaw said in Trouble assigning value to boolean in the struct:

                        since value() returns const

                        It does not return const. It's a non-mutating method, meaning that the vector's contents aren't going to be modified. It returns a copy of the element.

                        but copy of QString occurs (re my very unfortunate construction with assignment to QString) - will the value assigned to that QString remain in the QVector?

                        No. You're assigning to a temporary, which is going to be destroyed and assignment will have no observable effect (if the destructor of the class doesn't have side effects, which in this case it doesn't)

                        My understanding is that not, after terminating the method copy would be discarded and new value lost. Do I understand correctly?

                        That's correct.

                        Read and abide by the Qt Code of Conduct

                        artwawA 1 Reply Last reply
                        3
                        • kshegunovK kshegunov

                          @artwaw said in Trouble assigning value to boolean in the struct:

                          since value() returns const

                          It does not return const. It's a non-mutating method, meaning that the vector's contents aren't going to be modified. It returns a copy of the element.

                          but copy of QString occurs (re my very unfortunate construction with assignment to QString) - will the value assigned to that QString remain in the QVector?

                          No. You're assigning to a temporary, which is going to be destroyed and assignment will have no observable effect (if the destructor of the class doesn't have side effects, which in this case it doesn't)

                          My understanding is that not, after terminating the method copy would be discarded and new value lost. Do I understand correctly?

                          That's correct.

                          artwawA Offline
                          artwawA Offline
                          artwaw
                          wrote on last edited by
                          #32

                          @kshegunov Thank you, will remember that.

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          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