   Question about zero initialization
  Ok some code :template<typename T>struct Var{ T var;};The problem with that is, built in types wont be initialized to its initial value.To solve it we use the default constructor T(). So thus, template<typename T>struct Var{ T var; Var : var(T()){}};That above is the same as this right : T var = T(); Now the book says we can also do this :template<typename T>struct Var{ T var; Var : var(){}};I tried this and it works, but does the above amount to : T var();Rather than "does it amount it", I should ask why doen't it?Maybe this is a stupid question and I'm just over thinking it. Any help?   
  One of C++'s more annoying parsing rules is that anything that looks like a function declaration will be parsed as a function declaration. So if you type out T var(); the compiler won't see that as a variable definition, but rather a function declaration for a function named var that returns a T and doesn't take any parameters.   ;  In C++, it's a bad idea to try to talk about code X "amounting to" code Y because of the context sensitivity :)Within a function, e.g.template <typename T>int foo() {  T var;}the statement 'T var;' will declare the variable T and also construct it using the default constructor. This is different from the behaviour when 'T var;' is a member of a struct. (This is preserving old behaviour for optimization reasons; it used to be that you didn't want to pay the cost of initializing something until you could prove it would actually be used. Nowadays compilers can generally do that sort of optimization anyway; the initialization will eventually get identified as dead code.)If we write 'T var();' instead, then it's a function declaration - claiming that somewhere else, there will be a function named 'var' which accepts no parameters and returns a T. However, in the initialization list, ': var()' cannot be parsed as a function declaration (there is no "return value"), so it's correctly understood as "to initialize 'var' when calling this constructor, use the default constructor for T (the type of 'var')".The initialization list with ': var(T())' is literally saying "to initialize 'var', create an unnamed instance of T with the default constructor for T, and then use the copy constructor for T to copy that into 'var'". There's a pretty good chance that this will be optimized, too; but why say things in a complicated way when you don't have to? :)Also, none of this is anything to do with "zero initialization". Default construction is a different thing, and it is very often the case that you do not want the "default" state of an object to have all zero bits. Just a warning, because even in 2010 there is a lot of code floating around that uses memset(), ZeroMemory() etc. to "zero out" instances of classes, and it is almost always wrong-headed and sometimes quite dangerous.   ;Quote:Original post by ZahlmanJust a warning, because even in 2010 there is a lot of code floating around that uses memset(), ZeroMemory() etc. to "zero out" instances of classes, and it is almost always wrong-headed and sometimes quite dangerous.Virtual function tables? What virtual function tables? I didn't declare any virtual function tables... :P   