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. constant functions in Qt Classes
Forum Updated to NodeBB v4.3 + New Features

constant functions in Qt Classes

Scheduled Pinned Locked Moved Solved C++ Gurus
14 Posts 7 Posters 1.4k Views 2 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

    In QLabel Class; in public functions, there are following functions:
    Function 1: QString text() const
    Function 2: void setMargin(int)

    In what way Function1 differ from Function2 except the operations they perform?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • Swati777999S Swati777999

      In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

      In QLabel Class; in public functions, there are following functions:
      Function 1: QString text() const
      Function 2: void setMargin(int)

      In what way Function1 differ from Function2 except the operations they perform?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Swati777999 said in constant functions in Qt Classes:

      In what way Function1 differ from Function2 except the operations they perform?

      You should really read a C++ book!

      A const method is not allowed to change the state of the object on which it is called.
      That means that text() will not change the internal state of the object (it will not change any internal variables). If you try to change the state of the object in a const method you will get compiler error. Especially getter methods are usually const methods because they simply return the value of an internal variable without changing anything.
      A non-const method is allowed to change internal object state. For example setMargin(...) will change the margin in the object.

      Also, please ask such questions in https://forum.qt.io/category/34/c-gurus

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Swati777999S 1 Reply Last reply
      5
      • Swati777999S Swati777999

        In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

        In QLabel Class; in public functions, there are following functions:
        Function 1: QString text() const
        Function 2: void setMargin(int)

        In what way Function1 differ from Function2 except the operations they perform?

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

        @Swati777999 interpret it as readonly


        and moved to "c++ gurus"


        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.

        jsulmJ 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @Swati777999 interpret it as readonly


          and moved to "c++ gurus"

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @J-Hilk This is way shorter explanation than mine :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @Swati777999 said in constant functions in Qt Classes:

            In what way Function1 differ from Function2 except the operations they perform?

            You should really read a C++ book!

            A const method is not allowed to change the state of the object on which it is called.
            That means that text() will not change the internal state of the object (it will not change any internal variables). If you try to change the state of the object in a const method you will get compiler error. Especially getter methods are usually const methods because they simply return the value of an internal variable without changing anything.
            A non-const method is allowed to change internal object state. For example setMargin(...) will change the margin in the object.

            Also, please ask such questions in https://forum.qt.io/category/34/c-gurus

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #5

            @jsulm
            Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            jsulmJ JonBJ 2 Replies Last reply
            0
            • Swati777999S Swati777999

              @jsulm
              Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Swati777999 Qt is a C++ framework, so C++ syntax applies in C++ Qt applications. QML is based on JavaScript syntax. There are binding for Python also.
              What you asked in this thread has nothing to do with Qt but belongs to basic C++ syntax, so please use correct forum for such questions.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • Swati777999S Swati777999

                @jsulm
                Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

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

                @Swati777999
                Qt is not a combination of any languages. It is not a language, it is a library of functions. It is available for use from C++ or from Python (quite separately from one another). There is no JavaScript in anything you show, and JS is barely used in anything much from Qt.

                Your questions, or your question here, is just to do with C++. Nothing Qt about it.

                1 Reply Last reply
                3
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  https://www.geeksforgeeks.org/const-keyword-in-cpp/
                  const is a useful keyword in C++ and try to use it as much as possible whereever it is applied. It can avoid some silly bugs.

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

                    @JoeCFD said in constant functions in Qt Classes:

                    const is a useful keyword in C++

                    const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                    JKSHJ JoeCFDJ JonBJ 3 Replies Last reply
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      @JoeCFD said in constant functions in Qt Classes:

                      const is a useful keyword in C++

                      const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @Kent-Dorfman said in constant functions in Qt Classes:

                      @JoeCFD said in constant functions in Qt Classes:

                      const is a useful keyword in C++

                      const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                      It's not just for protection. It also conveys the designer's intent for the API and it facilitates optimizations.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

                        @JoeCFD said in constant functions in Qt Classes:

                        const is a useful keyword in C++

                        const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

                        @Kent-Dorfman I swallowed it without any side effects. I know you will hate it.

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

                          @JoeCFD said in constant functions in Qt Classes:

                          const is a useful keyword in C++

                          const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

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

                          @Kent-Dorfman said in constant functions in Qt Classes:

                          const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                          Wow! Did you start out with C when it had no const keyword?

                          Kent-DorfmanK 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Kent-Dorfman said in constant functions in Qt Classes:

                            const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                            Wow! Did you start out with C when it had no const keyword?

                            Kent-DorfmanK Offline
                            Kent-DorfmanK Offline
                            Kent-Dorfman
                            wrote on last edited by Kent-Dorfman
                            #13

                            @JonB

                            K&R C:

                            fn(a,b)
                            int a;
                            int b; 
                            {
                            /* stuff */
                            }
                            
                            JonBJ 1 Reply Last reply
                            0
                            • Kent-DorfmanK Kent-Dorfman

                              @JonB

                              K&R C:

                              fn(a,b)
                              int a;
                              int b; 
                              {
                              /* stuff */
                              }
                              
                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #14

                              @Kent-Dorfman
                              I'm with you :) Originally there wasn't any void either!

                              1 Reply Last reply
                              0

                              • Login

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