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. Where do you call base method in an override?

Where do you call base method in an override?

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 5 Posters 3.5k 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.
  • JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #1

    Let's say you are overriding a virtual base method with your own code, and the code you do does something of its own and you still want to call the base method. For example, I am doing something's mouseMoveEvent() override, I want to do something of my own unrelated to the base functionality, and I still want to call baseObject::mouseMoveEvent() for base behaviour.

    I started out calling it at the end of the overridden method, after I had done my own stuff. I don't know why, that just seemed natural.

    Now I find myself moving over to calling it at the start of the method. It gets it done at the head of the function while I think of it, and it means I can use a return statement in my code without worrying about still needing to call the base.

    Do you have a rule as to where you put the base call in your override code? Note that I have made clear the code is not going to interfere with the base functionality, nor does it rely on being executed either before or after whatever the base does, as those cases are obvious.

    J.HilkJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      If code in subclass really is completely 100% unrelated, then it does not matter much where you put it.

      And putting it on top so as not to forget about it seems like a good idea. Or, if you for some reason need to put it at end, you can use qScopeGuard and it will be called automatically.

      (Z(:^

      1 Reply Last reply
      5
      • JonBJ JonB

        Let's say you are overriding a virtual base method with your own code, and the code you do does something of its own and you still want to call the base method. For example, I am doing something's mouseMoveEvent() override, I want to do something of my own unrelated to the base functionality, and I still want to call baseObject::mouseMoveEvent() for base behaviour.

        I started out calling it at the end of the overridden method, after I had done my own stuff. I don't know why, that just seemed natural.

        Now I find myself moving over to calling it at the start of the method. It gets it done at the head of the function while I think of it, and it means I can use a return statement in my code without worrying about still needing to call the base.

        Do you have a rule as to where you put the base call in your override code? Note that I have made clear the code is not going to interfere with the base functionality, nor does it rely on being executed either before or after whatever the base does, as those cases are obvious.

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

        @JonB The only place, that comes to my mind, where the order matters would be paintEvent,

        if you cal it at the end, your previous drawing will be erased


        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.

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

          @JonB The only place, that comes to my mind, where the order matters would be paintEvent,

          if you cal it at the end, your previous drawing will be erased

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

          @J-Hilk
          Like I say, for this question I am only interested in what you experts choose to do when the order of your code vs the base class call does not matter/alter any behaviour. I realize you can then do either order, I am interested in which you choose to do, for whatever reason.

          @sierdzio
          Thank you for qScopeGuard, as you point out I could use that, it's useful to know. In my case I like KISS, so

          And putting it on top so as not to forget about it seems like a good idea.

          will do me. I don't know why I started out/used to go for putting it at the end of the function, I think I'll now stick to putting it at the start so it's done and I don't need to think about it again!

          J.HilkJ 1 Reply Last reply
          1
          • JonBJ JonB

            @J-Hilk
            Like I say, for this question I am only interested in what you experts choose to do when the order of your code vs the base class call does not matter/alter any behaviour. I realize you can then do either order, I am interested in which you choose to do, for whatever reason.

            @sierdzio
            Thank you for qScopeGuard, as you point out I could use that, it's useful to know. In my case I like KISS, so

            And putting it on top so as not to forget about it seems like a good idea.

            will do me. I don't know why I started out/used to go for putting it at the end of the function, I think I'll now stick to putting it at the start so it's done and I don't need to think about it again!

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

            @JonB said in Where do you call base method in an override?:

            @J-Hilk
            Like I say, for this question I am only interested in what you experts choose to do when the order of your code vs the base class call does not matter/alter any behaviour.

            oh, ok

            I'll now stick to putting it at the start so it's done and I don't need to think about it again!

            thats my philosophy as well :D


            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.

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

              Unless there is a reason otherwise, I generally call the base AFTER my own code, not withstanding the good points made above about things like paint events. Suits my sensibilities better to do my custom stuff first, then pass it on to the framework. It's kind of a control freak thing, I guess.

              If you meet the AI on the road, kill it.

              JonBJ 1 Reply Last reply
              1
              • Kent-DorfmanK Kent-Dorfman

                Unless there is a reason otherwise, I generally call the base AFTER my own code, not withstanding the good points made above about things like paint events. Suits my sensibilities better to do my custom stuff first, then pass it on to the framework. It's kind of a control freak thing, I guess.

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

                @Kent-Dorfman
                And as I wrote, until recently I was in your camp, for whatever reasons! But now I seem to be moving to the other camp, to get the base call out of the way. Hence I was just asking what others did.

                Thanks all for responses. Not surprisingly, it's a matter of choice, with different strokes for different folks.

                1 Reply Last reply
                1
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It's no matter of choice but a matter of what you want to achieve - do you want to modify the input for the function or the output.

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

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    It's no matter of choice but a matter of what you want to achieve - do you want to modify the input for the function or the output.

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

                    @Christian-Ehrlicher
                    ? I'm lost at what you are asking here. It is a matter of choice. I don't know what you mean by "modify the input for the function or the output". I'm not modifying anything, I took the example of mouseMoveEvent(). As I have made clear a couple of times, I am asking about those situations where my override code neither alters the base method behaviour nor relies on whether the base is called before or after.

                    In the simplest case

                    virtual void mouseMoveEvent(QMouseEvent *event) override
                    {
                        qDebug() << "mouseMoveEvent";
                    
                        QGraphicsView::mouseMoveEvent(event);
                    }
                    

                    versus

                    virtual void mouseMoveEvent(QMouseEvent *event) override
                    {
                        QGraphicsView::mouseMoveEvent(event);
                    
                        qDebug() << "mouseMoveEvent";
                    }
                    

                    That's all.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then it doesn't matter (if you really don't rely on the base impl - the mouse move event e.g. can change the hovered item so you have to make sure you really don't rely on the base impl)

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

                      JonBJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        Then it doesn't matter (if you really don't rely on the base impl - the mouse move event e.g. can change the hovered item so you have to make sure you really don't rely on the base impl)

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

                        @Christian-Ehrlicher said in Where do you call base method in an override?:

                        Then it doesn't matter

                        I know that, I said that from the very start of this question, e.g.

                        Like I say, for this question I am only interested in what you experts choose to do when the order of your code vs the base class call does not matter/alter any behaviour. I realize you can then do either order, I am interested in which you choose to do, for whatever reason.

                        That should have been pretty clear! :) I know it doesn't matter, I was asking which you do as a matter of preference/style. Just because I'm interested, and I'd like to learn what others choose....

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          I don't have a preference as long as there is no return value, then it's at the end for simplicity

                          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

                          • Login

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