Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved C++ 11 standards - initialize member variables in header

    General and Desktop
    qt5.7.0 c++11
    4
    7
    2597
    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.
    • S
      sayan275 last edited by

      Is it necessary to initialize member variables with nullptr or Q_NULLPTR in header files? If yes, why is it so required, when I do proper initialize it the ctor initialization list.

      in MyDialog.h,

      QDialog* m_Dialog = Q_NULLPTR;
      

      and in MyDialog.cpp...I do

      MDialog()::MDialog()
        : QDialog()
        , m_Dialog(new QDialog())
        { 
        }
      

      And in destructor, I do proper delete n setting it to nullptr.

      Why is the below required?

      QDialog* m_Dialog = Q_NULLPTR;
      
      sierdzio J.Hilk 2 Replies Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators @sayan275 last edited by

        @sayan275 said in C++ 11 standards - initialize member variables in header:

        Why is the below required?
        QDialog* m_Dialog = Q_NULLPTR;

        It is not required :-)

        (Z(:^

        1 Reply Last reply Reply Quote 2
        • J.Hilk
          J.Hilk Moderators @sayan275 last edited by

          @sayan275
          its like placing your hand in front of you nose, whilst sneezing.

          • Its polite, and I might just prevent headache later on.

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          sierdzio 1 Reply Last reply Reply Quote 4
          • sierdzio
            sierdzio Moderators @J.Hilk last edited by

            @J.Hilk said in C++ 11 standards - initialize member variables in header:

            @sayan275
            its like placing your hand in front of you nose, whilst sneezing.

            • Its polite, and I might just prevent headache later on.

            omg, best comment ever :D Very well put.

            (Z(:^

            S 1 Reply Last reply Reply Quote 1
            • S
              sayan275 @sierdzio last edited by

              @sierdzio yeah...haha

              aha_1980 1 Reply Last reply Reply Quote 0
              • aha_1980
                aha_1980 Lifetime Qt Champion @sayan275 last edited by

                @sayan275 honestly, I just love the C++11 member init. it will save you so much trouble once you add another constructor.

                and by doing 'find usages' I immediately see that the pointer always has a default value.

                that may be no problem in your simple example, but for more complex classes it really simplifies the readability.

                Qt has to stay free or it will die.

                S 1 Reply Last reply Reply Quote 4
                • S
                  sayan275 @aha_1980 last edited by

                  @aha_1980 Thanks!!

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