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. When to use class constructor member variable initializations?
Forum Updated to NodeBB v4.3 + New Features

When to use class constructor member variable initializations?

Scheduled Pinned Locked Moved Solved C++ Gurus
19 Posts 6 Posters 4.0k Views 5 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.
  • SGaistS SGaist

    Hi,

    There's a comma issue there ;-)

    I usually go with all member initialisation in that list.

    That also allows you to get a nice warning if you put them out of order.

    Note that with recent C++ standards, you can also do member initialisation at declaration time. See here for what is allowed.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #10

    @SGaist said in When to use class constructor member variable initializations?:

    Note that with recent C++ standards, you can also do member initialisation at declaration time. See here for what is allowed.

    When I first (years ago, certainly before C++11) did some C++, I used to write:

    class Foo
    {
        int z = 0;
    }
    

    but the guy who was a C++-er said to me: don't do it there, do it explicitly in the constructor. Are you now saying we can/should do initialization in the class declaration after all? Or have I misunderstood?

    1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @JonB said in When to use class constructor member variable initializations?:

      would you be kind enough to spell out exactly what you are saying for me?

      struct foo {
        std::string m_str;
      
        foo()
      }
      
      foo::foo()
      {
        m_str = "blub";
      }
      

      In this case in the ctor first std::string() is called by the compiler, then in the ctor body the string is assigned. When you do

      foo::foo() : m_string("blub") {}
      

      Then the std::string is assigned only once.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #11

      @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

      Then the std::string is assigned only once.

      Yes, I know that. But that does not apply if it's int m_i instead? So what things do or do not get this auto-initialization? I have to look at what feature about the type of every member variable to see where I am?

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #12

        @JonB said in When to use class constructor member variable initializations?:

        don't do it there, do it explicitly in the constructor

        This did not work before c++11

        So what things do or do not get this auto-initialization?

        They're undefined (You're correct for PODs)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @JonB said in When to use class constructor member variable initializations?:

          don't do it there, do it explicitly in the constructor

          This did not work before c++11

          So what things do or do not get this auto-initialization?

          They're undefined (You're correct for PODs)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #13

          @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

          This did not work before c++11

          What didn't work < C++ 11? class Foo { int z = 0; } didn't work??

          They're undefined (You're correct for PODs)

          So are you saying I need to go look up whale pods for C++ in order to understand what is/is not initialized for class members?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #14

            @JonB said in When to use class constructor member variable initializations?:

            I need to go look up whale pods for C++

            See http://www.cplusplus.com/reference/type_traits/is_pod/

            What didn't work < C++ 11? class Foo { int z = 0; } didn't work??

            Correct

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @JonB said in When to use class constructor member variable initializations?:

              I need to go look up whale pods for C++

              See http://www.cplusplus.com/reference/type_traits/is_pod/

              What didn't work < C++ 11? class Foo { int z = 0; } didn't work??

              Correct

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #15

              @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

              Correct

              Indeed! Gosh, I did not know that, I thought it did work < C++11. Clearly my memory is not to be relied on.

              I ended up reading https://www.boost.org/doc/libs/1_65_1/libs/utility/value_init.htm. Which goes through all this initializer stuff, and made my head hurt.

              I realize that, not for the first time, I am in a minority of one not liking any of this stuff. I was getting to quite like C++ over the past months, despite my misgivings of the past. Now I'm remembering just why I found it arcane [e.g. "C++ has at least four different initialization notations, some of which overlap."].. :(

              kshegunovK jeremy_kJ 2 Replies Last reply
              0
              • JonBJ JonB

                @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

                Correct

                Indeed! Gosh, I did not know that, I thought it did work < C++11. Clearly my memory is not to be relied on.

                I ended up reading https://www.boost.org/doc/libs/1_65_1/libs/utility/value_init.htm. Which goes through all this initializer stuff, and made my head hurt.

                I realize that, not for the first time, I am in a minority of one not liking any of this stuff. I was getting to quite like C++ over the past months, despite my misgivings of the past. Now I'm remembering just why I found it arcane [e.g. "C++ has at least four different initialization notations, some of which overlap."].. :(

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #16

                You know nothing Jon B. Actually, C++ was decently complicated to begin with, and is getting more complicated by the day. So the problem is trivial: when is the old stuff going to be cleaned up and the new stuff simplified? To which I suppose the answer's 'never'. How's that for a cheery perspective?

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

                  Correct

                  Indeed! Gosh, I did not know that, I thought it did work < C++11. Clearly my memory is not to be relied on.

                  I ended up reading https://www.boost.org/doc/libs/1_65_1/libs/utility/value_init.htm. Which goes through all this initializer stuff, and made my head hurt.

                  I realize that, not for the first time, I am in a minority of one not liking any of this stuff. I was getting to quite like C++ over the past months, despite my misgivings of the past. Now I'm remembering just why I found it arcane [e.g. "C++ has at least four different initialization notations, some of which overlap."].. :(

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #17

                  @JonB said in When to use class constructor member variable initializations?:

                  @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

                  Correct

                  Indeed! Gosh, I did not know that, I thought it did work < C++11. Clearly my memory is not to be relied on.

                  It's possible that the compiler you used implemented this behavior as a non-standard extension. There were, and still are lots of cases of non-standard code having perfectly reasonable, compiler defined behavior.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  JonBJ 1 Reply Last reply
                  0
                  • jeremy_kJ jeremy_k

                    @JonB said in When to use class constructor member variable initializations?:

                    @Christian-Ehrlicher said in When to use class constructor member variable initializations?:

                    Correct

                    Indeed! Gosh, I did not know that, I thought it did work < C++11. Clearly my memory is not to be relied on.

                    It's possible that the compiler you used implemented this behavior as a non-standard extension. There were, and still are lots of cases of non-standard code having perfectly reasonable, compiler defined behavior.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #18

                    @jeremy_k
                    In those days I --- or, to be accurate, much of the time my colleague, whose code I would have looked at --- would have been using Visual Studio/MSVC, say VS2010 plus its compiler. It's possible that might have allowed these initializations before the C++11 standard. Equally possible is that I just misremember ;-)

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

                      I'd suggest that a class with many instance vars (traits) often indicates a design error and less than optimal use of subclassing. IOW, trying to do too much in a single scope. This is often the result of incremental development, without a clear architecture proposal before coding....not always, but often.

                      It's not critical to initialize vars by the initializer list, but do initialize them in proper order. I often defer trait initialization to a private Init() method because it looks cleaner. At least in recent c++ versions you can do nested constructors.

                      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