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. How to ensure comparison operator is "complete"?
QtWS25 Last Chance

How to ensure comparison operator is "complete"?

Scheduled Pinned Locked Moved C++ Gurus
13 Posts 4 Posters 5.1k 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
    Asperamanca
    wrote on last edited by
    #1

    Let's say I have a class with n member variables. I want to provide a comparison operator for the whole class.

    How can I ensure I do not forget about the operator== when adding members later on, or otherwise changing the class? Same goes for the copy constructor, BTW.

    Usually, it's no big deal. But there's always a time when you change code in a hurry, and later on wonder why some other perfectly fine code doesn't do as expected. I'd like to have some kind of safety net.

    Ideas?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      -Rely on the automatically generated ones?-

      Edit:
      If your classes derive from QObject, you could use Qt's property system to do your comparisons. If you expose all relevant properties via Q_PROPERTY, your implementation of the comparison operator could iterate over these properties and always work, even for subclasses. However, that has two downsides:

      • It only move the responsibility of keeping the operators in sync to making sure all properties are Q_PROPERYs
      • It would run slower than using direct comparisons.

      I can't think of another way though.

      Edit 2:
      There are no default generated ones, thanks for correcting me.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        I often have non-trivial comparisons, like fuzzyCompare for qreal values.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          How should any algorithm know whether a new attribute in your class is relevant for a comparison?

          Even if the answer in your case would be "any one is relevant", that would be out of the scope of a compiler. One would have to analyze the source code and then to analyze the operator== method.

          The method Andre did outline, works too. But it cannot handle the cases where your forgot to make the new attribute a property. So you're not gaining that much from that.

          For our projects, I just would add a HUGE comment at the place where the attributes are added in the header:

          @
          /************************

          • CAUTION!
          • if you add an attribute here
          • ADD IT TO THE comparison methods too!
            ************************/
            @

          :-)

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            I was afraid this would be the answer...

            well, one could hope.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Yes, hope dies last :-)

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Perhaps you can find a static code analysis tool that can warn for these cases?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by
                  #8

                  Yes, I also thought this would be in the realm of static code analysis.
                  If anyone knows about a static code analysis tool that can provide that, please speak up.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rwst
                    wrote on last edited by
                    #9

                    Maybe this is a stupid question but do you need the operator==() for comparison?
                    (edit:I meant to say, do you need == within the comparison?)
                    It could be that it suffices for your application to compare the allocated memory
                    blocks using sizeof(Class1/2) and memcmp()? OK, this is C not C++...

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      As the topic starter stated, he needs to do non-trival comparisons, like fuzzy comparisons of reals. That is never going to work with doing a memcmp. -Otherwise, relying on the default comparison operator would have been just as effective.-

                      Edit: there is no such thing as a default comparison operator in C++, thanks for correcting me.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Asperamanca
                        wrote on last edited by
                        #11

                        [quote author="Andre" date="1333107436"]Rely on the automatically generated ones?[/quote]

                        Took me a while to notice...since when do automatically generated comparison operators exist?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          [quote author="Asperamanca" date="1334563036"][quote author="Andre" date="1333107436"]Rely on the automatically generated ones?[/quote]

                          Took me a while to notice...since when do automatically generated comparison operators exist?

                          [/quote]

                          Sorry, you are right: they don't. I thought there were, but I was wrong on this count. I stand corrected.

                          StackOverflow has an interesting "discusson":http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator on the why (not) of automatic operator==() definition.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Asperamanca
                            wrote on last edited by
                            #13

                            I think it's the same one I stumbled upon. ;-)

                            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