Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Clean constructors
QtWS25 Last Chance

Clean constructors

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 4.7k 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.
  • K Offline
    K Offline
    kibsoft
    wrote on last edited by
    #1

    Hi everyone. I saw such line in the Qt Essentials Curriculum Block: "Understand the importance of clean constructors" (Writing your own widgets).

    So, what means "clean constructor"?

    1 Reply Last reply
    0
    • frankcyblogic.deF Offline
      frankcyblogic.deF Offline
      frankcyblogic.de
      wrote on last edited by
      #2

      Good question. I think it means how to write constructors correctly. Like:
      @
      explicit MyWidget(QWidget* parent, ...etc): BaseWidget(parent) {}
      @

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        My understanding of it is to just have constructors that are simple and only take a very small number of arguments (if any).

        For example it is better to have code like:

        @
        ProgressBar* progress = new ProgressBar( this );
        progress->setRange( 0, 1000 );
        progress->setValue( 230 );
        @

        as opposed to

        @
        ProgressBar* progress = new ProgressBar( 0, 1000, 230, this );
        @

        as you just end up with a bunch of meaningless numbers in your code. With the former you can see the numbers in the context of the member function which gives a clue as to their meaning.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kibsoft
          wrote on last edited by
          #4

          I was inclined to

          bq. explicit MyWidget(QWidget* parent, ...etc): BaseWidget(parent) {}

          Maybe Trolls themselves will answer? What is correct?

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            I was thinking of this "article":http://doc.qt.nokia.com/qq/qq13-apis.html#theconveniencetrap when I wrote my reply.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              In the Qt API, it is standard that the parent argument should be last, and should have a default value of 0. So it would be
              @
              MyWidget(int realImportantArgument, QWidget* parent = 0);
              @

              The article ZapB links to is insightful if you want to learn about API design.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VC15
                wrote on last edited by
                #7

                Looks like that Qt Essentials Curriculum Block is not perfect. I see that certified specialists don't give an exact answer but explain only their own understanding of the term "clean constructor".
                Therefore I realize that there isn't a documented definition of this term.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Would you mind giving it then? You seem in the know?

                  Note that this, IMHO, does not belong in the essentials curriculum at all. Perhaps in the C++ for Qt one though.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VC15
                    wrote on last edited by
                    #9

                    Unfortunately, I also don't know the proper definition. I found this topic searching on the Internet to find it.
                    And as you can see at http://qt.nokia.com/developer/learning/certification/exams/preparation-prerequisites/qt-curriculum/qt-essentials clean constructors are in the Essentials block (See section "Writing your own widgets")

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Here are some more links that provide guidelines:

                      • http://doc.qt.nokia.com/qq/qq13-apis.html
                      • http://developer.qt.nokia.com/wiki/Coding_Conventions
                      • http://developer.qt.nokia.com/wiki/API_Design_Principles
                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        VC15
                        wrote on last edited by
                        #11

                        Oh, great! These articles look to be very useful. Thanks a lot!

                        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