Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is there a reason you would initialise variables like this?

Is there a reason you would initialise variables like this?

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestionannouncement
17 Posts 9 Posters 0 Views 1 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.
  • M Offline
    M Offline
    MarkB777
    wrote on last edited by
    #1

    Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

    Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

    _ L G E M 5 Replies Last reply
    0
    • M MarkB777

      Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

      Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      Const and reference members can only be initialized in an intializer list. Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again. Hence, there would be two calls for variable initialization

      You talk about Being HUMAN. I have it in my name AnsHUMAN

      J 1 Reply Last reply
      0
      • M MarkB777

        Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

        Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It's just a matter of style, although I think the first option is preferred. MSDN has this to say[^] on the matter. You may also like to check if Bjarne Stroustrup has more to offer.

        One of these days I'm going to think of a really clever signature.

        1 Reply Last reply
        0
        • _ _AnsHUMAN_

          Const and reference members can only be initialized in an intializer list. Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again. Hence, there would be two calls for variable initialization

          You talk about Being HUMAN. I have it in my name AnsHUMAN

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          _AnsHUMAN_ wrote:

          Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.

          I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.

          P W 2 Replies Last reply
          0
          • M MarkB777

            Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

            Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

            G Offline
            G Offline
            George L Jackson
            wrote on last edited by
            #5

            C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].

            "We make a living by what we get, we make a life by what we give." --Winston Churchill

            M W 2 Replies Last reply
            0
            • M MarkB777

              Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

              Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

              E Offline
              E Offline
              Eugen Podsypalnikov
              wrote on last edited by
              #6

              // Is there any particular reason Yes :) :

              class A
              {
              //..
              public:
              A();
              A(int);
              A& operator=(int);
              };

              class B
              {
              A m_a;
              public:
              B() { m_a = 3; /*second call, after A::A()*/}
              // B() : m_a(3) /*first and last call of A::A(int)*/ {}
              };

              They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

              1 Reply Last reply
              0
              • J jschell

                _AnsHUMAN_ wrote:

                Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.

                I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.

                P Offline
                P Offline
                Philippe Mori
                wrote on last edited by
                #7

                In standard C++, member without default constructor are not initialized to 0 automatically but are random.

                Philippe Mori

                J 1 Reply Last reply
                0
                • M MarkB777

                  Hi all, Is there any particular reason you would initialise private members of a class like this... SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { } As opposed to this? SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; } From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, Mark

                  Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

                  M Offline
                  M Offline
                  Malli_S
                  wrote on last edited by
                  #8

                  The difference is that, in the first way of initialization (known as 'Initialization List'), the constructors of the initialized members are called. While in the second option, all the objects get constructed before reaching the first executable statement of the constructor. Hence in the second way of initialization, the assignment operators gets called.

                  [Delegates]      [Virtual Desktop]      [Tray Me !]
                  -Malli...! :rose:****

                  1 Reply Last reply
                  0
                  • G George L Jackson

                    C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].

                    "We make a living by what we get, we make a life by what we give." --Winston Churchill

                    M Offline
                    M Offline
                    MarkB777
                    wrote on last edited by
                    #9

                    Thanks George :)

                    Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

                    1 Reply Last reply
                    0
                    • J jschell

                      _AnsHUMAN_ wrote:

                      Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.

                      I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.

                      W Offline
                      W Offline
                      w peuker
                      wrote on last edited by
                      #10

                      Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one). Of course, if your members have default constructors, they will be called (before the constructor body) unless you place them in the initializer list.

                      J 1 Reply Last reply
                      0
                      • G George L Jackson

                        C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].

                        "We make a living by what we get, we make a life by what we give." --Winston Churchill

                        W Offline
                        W Offline
                        w peuker
                        wrote on last edited by
                        #11

                        ...and don't forget that the compiler processes the initializer list following the declaration order given in the class definition. So you better stick to that order in the list (to prevent confusion). There are tools availabe that check this (e.g. cppcheck)

                        1 Reply Last reply
                        0
                        • P Philippe Mori

                          In standard C++, member without default constructor are not initialized to 0 automatically but are random.

                          Philippe Mori

                          J Offline
                          J Offline
                          jschell
                          wrote on last edited by
                          #12

                          Philippe Mori wrote:

                          In standard C++, member without default constructor are not initialized to 0 automatically but are random.

                          Wrong. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.

                          class Any
                          {
                          ....
                          int var1 = 2; // This is an initializer.
                          int var2; // No initializer so set to default value (zero.)
                          }

                          Local variables (those declared in a method) have not such default value.

                          P 1 Reply Last reply
                          0
                          • W w peuker

                            Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one). Of course, if your members have default constructors, they will be called (before the constructor body) unless you place them in the initializer list.

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #13

                            w-peuker wrote:

                            Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one).

                            Wrong. The only compilers that leave data members non-initialized would be compilers that are not implementing the ANSI C++ specification. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.

                            class Any
                            {
                            ....
                            int var1 = 2; // This is an initializer.
                            int var2; // No initializer so set to default value (zero.)
                            }

                            This happens regardless of how construction occurs.

                            P 1 Reply Last reply
                            0
                            • J jschell

                              Philippe Mori wrote:

                              In standard C++, member without default constructor are not initialized to 0 automatically but are random.

                              Wrong. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.

                              class Any
                              {
                              ....
                              int var1 = 2; // This is an initializer.
                              int var2; // No initializer so set to default value (zero.)
                              }

                              Local variables (those declared in a method) have not such default value.

                              P Offline
                              P Offline
                              Philippe Mori
                              wrote on last edited by
                              #14

                              At this time, I program mainly in C# and in that case meber are initialized to (as member of a ref class would also be initialized in C++/CLI). As far as I can tell, you have misunderstood the standard. A static member will be initialized to 0 but not a regiular member in the general case. I have found many link that confirm what I have said. http://www.cplusplus.com/forum/general/17582/[^] http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default[^] http://stackoverflow.com/questions/2614809/what-is-the-default-value-for-c-class-members[^] http://msdn.microsoft.com/en-us/library/s0wk5dy9.aspx[^]

                              Philippe Mori

                              J 1 Reply Last reply
                              0
                              • J jschell

                                w-peuker wrote:

                                Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one).

                                Wrong. The only compilers that leave data members non-initialized would be compilers that are not implementing the ANSI C++ specification. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.

                                class Any
                                {
                                ....
                                int var1 = 2; // This is an initializer.
                                int var2; // No initializer so set to default value (zero.)
                                }

                                This happens regardless of how construction occurs.

                                P Offline
                                P Offline
                                Philippe Mori
                                wrote on last edited by
                                #15

                                See my answer above. You have misunderstood the standard. I have found many links that proove otherwise. By the way, your example is not even valid C++ code.

                                Philippe Mori

                                J 1 Reply Last reply
                                0
                                • P Philippe Mori

                                  At this time, I program mainly in C# and in that case meber are initialized to (as member of a ref class would also be initialized in C++/CLI). As far as I can tell, you have misunderstood the standard. A static member will be initialized to 0 but not a regiular member in the general case. I have found many link that confirm what I have said. http://www.cplusplus.com/forum/general/17582/[^] http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default[^] http://stackoverflow.com/questions/2614809/what-is-the-default-value-for-c-class-members[^] http://msdn.microsoft.com/en-us/library/s0wk5dy9.aspx[^]

                                  Philippe Mori

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #16

                                  Philippe Mori wrote:

                                  As far as I can tell, you have misunderstood the standard.

                                  Yep, you are correct.

                                  1 Reply Last reply
                                  0
                                  • P Philippe Mori

                                    See my answer above. You have misunderstood the standard. I have found many links that proove otherwise. By the way, your example is not even valid C++ code.

                                    Philippe Mori

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #17

                                    Philippe Mori wrote:

                                    See my answer above. You have misunderstood the standard.

                                    Yes, you are correct.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups