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. Why "override" ? (Repost)

Why "override" ? (Repost)

Scheduled Pinned Locked Moved Unsolved C++ Gurus
14 Posts 5 Posters 2.4k 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 Anonymous_Banned275
    14 Oct 2022, 15:49

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

    C Offline
    C Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on 14 Oct 2022, 18:11 last edited by Chris Kawa
    #5

    @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 implement mousePressEvent in your class you are overriding (replacing) the base class implementation, so the word override 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.

    A 1 Reply Last reply 15 Oct 2022, 02:02
    2
    • C Chris Kawa
      14 Oct 2022, 18:11

      @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 implement mousePressEvent in your class you are overriding (replacing) the base class implementation, so the word override 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.

      A Offline
      A Offline
      Anonymous_Banned275
      wrote on 15 Oct 2022, 02:02 last edited by
      #6

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

      C 1 Reply Last reply 15 Oct 2022, 07:43
      0
      • A Anonymous_Banned275
        15 Oct 2022, 02:02

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

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 15 Oct 2022, 07:43 last edited by
        #7

        @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

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

        A F 2 Replies Last reply 15 Oct 2022, 12:39
        1
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 15 Oct 2022, 10:49 last edited by Chris Kawa
          #8

          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.

          A 1 Reply Last reply 15 Oct 2022, 12:34
          3
          • C Chris Kawa
            15 Oct 2022, 10:49

            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.

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on 15 Oct 2022, 12:34 last edited by
            #9

            @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 ?

            C 1 Reply Last reply 15 Oct 2022, 12:56
            0
            • C Christian Ehrlicher
              15 Oct 2022, 07:43

              @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

              A Offline
              A Offline
              Anonymous_Banned275
              wrote on 15 Oct 2022, 12:39 last edited by
              #10

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

              1 Reply Last reply
              0
              • A Anonymous_Banned275
                15 Oct 2022, 12:34

                @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 ?

                C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 15 Oct 2022, 12:56 last edited by
                #11

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

                1 Reply Last reply
                1
                • C Christian Ehrlicher
                  15 Oct 2022, 07:43

                  @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

                  F Offline
                  F Offline
                  fcarney
                  wrote on 17 Oct 2022, 18:14 last edited by
                  #12

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

                  C++ is a perfectly valid school of magic.

                  C 1 Reply Last reply 17 Oct 2022, 21:20
                  0
                  • F fcarney
                    17 Oct 2022, 18:14

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

                    C Offline
                    C Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 17 Oct 2022, 21:20 last edited by Chris Kawa
                    #13

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

                    1 Reply Last reply
                    1
                    • K Offline
                      K Offline
                      Kent-Dorfman
                      wrote on 18 Oct 2022, 05:16 last edited by
                      #14

                      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!"

                      1 Reply Last reply
                      0

                      14/14

                      18 Oct 2022, 05:16

                      • Login

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