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. C++ 11 standards - initialize member variables in header
QtWS25 Last Chance

C++ 11 standards - initialize member variables in header

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.7.0c++11
7 Posts 4 Posters 3.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.
  • S Offline
    S Offline
    sayan275
    wrote on 12 Apr 2018, 05:45 last edited by
    #1

    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;
    
    S J 2 Replies Last reply 12 Apr 2018, 06:16
    0
    • S sayan275
      12 Apr 2018, 05:45

      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;
      
      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 12 Apr 2018, 06:16 last edited by
      #2

      @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
      2
      • S sayan275
        12 Apr 2018, 05:45

        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;
        
        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 12 Apr 2018, 06:19 last edited by
        #3

        @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


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

        S 1 Reply Last reply 12 Apr 2018, 06:22
        4
        • J J.Hilk
          12 Apr 2018, 06:19

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

          • Its polite, and I might just prevent headache later on.
          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 12 Apr 2018, 06:22 last edited by
          #4

          @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 12 Apr 2018, 06:36
          1
          • S sierdzio
            12 Apr 2018, 06:22

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

            S Offline
            S Offline
            sayan275
            wrote on 12 Apr 2018, 06:36 last edited by
            #5

            @sierdzio yeah...haha

            A 1 Reply Last reply 12 Apr 2018, 06:54
            0
            • S sayan275
              12 Apr 2018, 06:36

              @sierdzio yeah...haha

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 12 Apr 2018, 06:54 last edited by
              #6

              @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 12 Apr 2018, 08:32
              4
              • A aha_1980
                12 Apr 2018, 06:54

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

                S Offline
                S Offline
                sayan275
                wrote on 12 Apr 2018, 08:32 last edited by
                #7

                @aha_1980 Thanks!!

                1 Reply Last reply
                1

                3/7

                12 Apr 2018, 06:19

                • Login

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