   (c++) auto-initialize basic variables?
  HiI add loads of variables like int, bool float to my classes as i go along, and most of the time i need them to start at value 0. But setting this manually in my constuctor is kinda time consuming as i add a lot.Now, i know c++ doesnt auto-init them to 0 (like in java) to optimize but is there any way to override this or otherwise achieve this? I wouldnt mind the small waste of cpu it could be in those cases i need to init it to something other then 0 and it would speed up development.ThanksErik   
  The best way to initialize class member variables is by using the initialization list.  It looks like this.class SomeClass{public:   SomeClass();   int memberInt;   float memberFloat;   SomeOtherClass* memberPtr;};SomeClass::SomeClass(): memberInt(0), // <--- Initialization list  memberFloat(0.0f),   memberPtr(NULL){}Now, if you have too many members in your class and your initialization list is getting too long, you need to re-think your class design.  Break it up into smaller classes and combine them using composition.   ;  how is this much different from just init them in the constructor the normal way? I still need to add it at two places for every new int,float etc.Im asking if there is any way to set it up so i JUST add it in the class, and it's still inited to 0 value.I understand i can create a class "class myInt, class myFloat" etc that has a default constructor that sets it to 0 but i want something more streamlined. Is it possible in c++?   ;  I don't know of any way to do what you're asking, but if you find yourself using so many variables in a class that the physical act of typing up the constructor initialization list becomes time consuming, you might want to rethink your design. Is your class really serving a single, focused purpose? If not, consider breaking it up in to smaller classes and combining them in whatever method is appropriate.   ;  If you want to save a few characters you can also write:SomeClass::SomeClass(): memberInt(), // <--- Initialization list  memberFloat(), // <-- () means zero/NULL  memberPtr(){}I've always used initialisation lists like above, but if you really want to put the default value in the header, you can write a template class that allows something like:class SomeClass{public:  Default<int>m_IntA;//will be initialised to 0  Default<int, 42> m_IntB;//will be initialised to 42}This is off the top of my head (not tested), but the template would look something like:template<class T, T init=T()> class Default{public:  Default() : value(init) {}  operator  T&()  { return value; }  operator const T&() const { return value; }  T value;};;Quote:Original post by sulimanhow is this much different from just init them in the constructor the normal way?From the standpoint of saving time, it's not much different. The difference is that the initialization list initializes variables while the constructor body assigns to them. The latter is slower because the member variables are initialized to some junk value before being assigned to.;Quote:Original post by HodgmanI've always used initialisation lists like above, but if you really want to put the default value in the header, you can write a template class that allows something like...That's creative as hell. Neat trick :)   ;  Slightly off topic, but I have coded an expression templates implementation where I wanted default initialization by default, but also make code like ...Foo foo; // expression template, initialization is wasteful herefoo = bar + x * y;.. performant by concept (for some nontrivial cases where initialization is not possible or non-kiss).I have added an additional ctor-overload so I could writeFoo foo (Foo::noinit); // won't initialize member data(noinit is declared as member, like enum noinit_{}noinit)Don't know if such paradigm is common somewhere, but I call it the "no-init paradigm", yay.   ;Quote:Original post by AristeQuote:Original post by sulimanhow is this much different from just init them in the constructor the normal way?From the standpoint of saving time, it's not much different. The difference is that the initialization list initializes variables while the constructor body assigns to them. The latter is slower because the member variables are initialized to some junk value before being assigned to.Actually (except for debug builds) they aren't initialized to some junk value - they're left as a junk value - a [grin]very[grin] fast operation. The reason that then setting them in the body instead of an initializer list is slower is because the compiler can't simply fill the class with a fast memset operation. Except that it can - it will see you setting things in the body that could be done in the initializer list, and pretend that you did. Of course, this only applies to classes containing POD types.(Disclaimer: my C++ compiler optimization knowledge is rather rusty.)Of course, the OP could override new or some other crazy thing to get the desired effect. But please for the love of god do not do this. You simply have to get used to the fact that this is how you program in C++. (If you don't like it, use Java or C#.)(Story time: My first C++ compiler would always initialize to zero - even in release. This caused me no end of misery, unlearning the bad habit of relying on this feature, when I switched to a better compiler. It also made all my old code unusable. So please don't try it - just learn how to write proper C++ code in the first place.)   ;Quote:Original post by Andrew RussellStory time: My first C++ compiler would always initialize to zero - even in release.Out of curiosity, which one was this?   