Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Where do you call base method in an override?



  • 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.


  • Moderators

    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.


  • Moderators

    @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



  • @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!


  • Moderators

    @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



  • 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.



  • @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.


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

    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)



  • @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....


  • Lifetime Qt Champion

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


Log in to reply