Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Clean constructors

    General and Desktop
    5
    11
    4194
    Loading More Posts
    • 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
      kibsoft last edited by

      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 Reply Quote 0
      • F
        frankcyblogic.de last edited by

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

        1 Reply Last reply Reply Quote 0
        • Z
          ZapB last edited by

          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 Reply Quote 0
          • K
            kibsoft last edited by

            I was inclined to

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

            Maybe Trolls themselves will answer? What is correct?

            1 Reply Last reply Reply Quote 0
            • Z
              ZapB last edited by

              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 Reply Quote 0
              • A
                andre last edited by

                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 Reply Quote 0
                • V
                  VC15 last edited by

                  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 Reply Quote 0
                  • A
                    andre last edited by

                    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 Reply Quote 0
                    • V
                      VC15 last edited by

                      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 Reply Quote 0
                      • A
                        andre last edited by

                        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 Reply Quote 0
                        • V
                          VC15 last edited by

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

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post