Solved Where do you call base method in an override?
-
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. -
@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 forqScopeGuard
, as you point out I could use that, it's useful to know. In my case I like KISS, soAnd 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!
-
@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.
-
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 ofmouseMoveEvent()
. 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.
-
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....
-
I don't have a preference as long as there is no return value, then it's at the end for simplicity