Why "override" ? (Repost)
-
I am adding few "event(s" to QTextEdit and they all require "override" . Seems redundant.
Why?protected:
void closeEvent(QCloseEvent *event) override;
void mousePressEvent(QMouseEvent * event) override;
void mouseMoveEvent( QMouseEvent * event ) override ;
//add OCt14
void dropEvent(QDropEvent *event)override;
void dragEnterEvent(QDragEnterEvent *event)override;
void dragMoveEvent(QDragMoveEvent *event)override ; -
You mean the
override
keyword? It's not required, it is optional, but highly highly recommended as it's a great addition to the language.First it improves readability - reading someone else's code you know at a glance that this is an overridden virtual method.
It's also invaluable to help spot typos that everyone does from time to time. For example if you type
mousePresEvent
instead ofmousePressEvent
, without the override specifier it will happily compile and you'll be wasting time debugging why it isn't called. With the keyword you immediately get a compilation error saying there's no such virtual function to override. -
@Chris-Kawa No , I do not have an option - the editor ask to add "override" right after I type in " void mousePressEvent(QMouseEvent * event) " ) I am under (wrong) impression that "mousePressEvent"
is "native" to QTextEdit , I really did not research that, I am just guessing. I do not get why I have to use "override" .
Perhaps my English interpretation of "override" is not " to replace something ". -
@AnneRanch said in Why "override" ? (Repost):
I do not get why I have to use "override" .
You don't have to use it but you should use a standard c++11 feature to avoid possible errors when overiding a function as @Chris-Kawa already explained to you.
-
@AnneRanch said:
I am under (wrong) impression that "mousePressEvent" is "native" to QTextEdit
mousePressEvent
is a virtual method of a base class QWidget. QTextEdit is a subclass of QWidget and your class is a subclass of QTextEdit, so when you implementmousePressEvent
in your class you are overriding (replacing) the base class implementation, so the wordoverride
does exactly what it means.No , I do not have an option - the editor ask to add "override"
I meant that C++ syntax does not require it, not that you have an option in the editor. The editor is adding it because it's a good practice, but it's your code and your text. You are free to delete that keyword in text if you don't want it and it will work fine. You also don't have to use the editor wizard to add that method. You can just write it yourself. But again,
override
keyword is a good feature so I suggest to use it. -
@Chris-Kawa So is "subclass" same as "inheritance"?
Secondly - I have not tried to compile my code without adding "override" ... basically I could treat editor note as "warning / suggestion".
Not so sure I agree with that- it reminds me of "three level logic " - yes/ no and maybe.
PS I am happy with current version of C++ , I do not need to introduce more "update/ upgrade" problems.
-
@AnneRanch said in Why "override" ? (Repost):
PS I am happy with current version of C++ , I do not need to introduce more "update/ upgrade" problems.
Maybe you can try to convince the iso c++ standards comittee that they stop with adding new features to c++ lol
-
So is "subclass" same as "inheritance"?
Yes, subclassing and inheriting from another class means the same thing. Those two terms just come from different historical sources.
it reminds me of "three level logic "
The problem is C++ cares deeply about backwards compatibility. The committee could've make the
override
keyword mandatory, but that would break all the billions lines of code existing in the world that would suddenly stop compiling or worse - compile but work differently. So instead the keyword was made optional but highly encouraged and tools like editors follow that recommendation.I am happy with current version of C++
Current version is C++20 (with C++23 just around a corner) and override has been a feature added in C++11, so over a decade ago. It's a proven, widely used and very helpful addition. As for upgrading problems - there's nothing to upgrade. It's already there and has been for a long time.
-
@Chris-Kawa Many years ago I took a class in C programming. I recall the instructor was often referring to "ANSI C " book.
Is there such book today ? -
@Christian-Ehrlicher That would be a disaster for many "forums" - the number of silly questions - just like this one I posted, would screw up their statistics.
-
@AnneRanch said:
Is there such book today ?
C is a very small language compared to C++. There's hundreds of books covering different aspects of C++.
A good introductory book is "A Tour of C++ (3rd edition)" by Bjarne Stroustrup, but it's far too much material to cover in a single book. For list of features and raw syntax a good website with search capabilities is far more useful - cppreference. -
@Christian-Ehrlicher I think it would be interesting to have a flag that compiles a more modern subset of C++. It would be interesting to use that mode to learn the new stuff I am ignoring. Almost like a TypeScript mode, but for C++.
-
@fcarney The new stuff after C++98 is almost entirely additive, so you can choose a standard version to compile in, but it's not possible to use only the new stuff because there's just not enough of it e.g. you wouldn't want to write entire program using just lambdas. You want the good old functions and methods too.
There's just no committee in the world that would agree on what such subset would be. For example there's dozens of ways to initialize a variable, and that was back in 2018. Now it's probably two times more. Good luck picking the ones that would stay.
But, if you're interested in a "new C++" option, there's an emerging trend to invent a modernized language based on C++. The two most prominent projects are a recently announced cppfront, which aims to do the same cfront did to move from C to C++ back in the day, and Carbon. They're not really production ready yet and I doubt they will be for some years at least, but it's something to play with if you're into this sort of thing.
-
arguably there have been some good enhancements to C++ but there is also way too much syntactic fluff that creates more religions for the zealots to jihad over. IMMHO, way too much effort to "pythonize" the language. In my world: embedded systems, I need to see a clear line from assembler code to abstract algorithms in c++...some of the modern fluff makes disecting the code a painful experience...and too often it's a moving target to "keep up" with the latest officially blessed method for accomplishing something.
To quote "Real Programmers Don't Use PASCAL" (Datamation, 1982) -- "What you see is what you get is a bad concept...we want You asked for it, you got it!"