I was testing out something with statics. My former experience says statics shouldnt be get initialized inside constructor. Thats why Static Constructor concept exist.
Yesterday to test something, i created a class that contains a static variable. It shouldn't get initialized but it did. I tested it with many ways, but everytime it got initialized.
So do i know wrong, or something has changed with C++ that i am not aware?
There is no such thing as a static constructor. The constructors in your program are just regular constructors that are called when you create an object.
what's wrong is you are initializing it in your constructors (lines 24 and 30). Get rid of those two and your var1 will remain zeroed.
You can see that it is statically initialized by printing its value before you create any instances of Foo.
What it looks like you 'want' to do is have a static variable that indicates whether you have initialized the var1. If not, ctors should init it and set flag var to 'already initialized'.
Static variables are initialized to zero by default. You don't specify any value when you define var1 on line 18 so it gets initialized to 0.
Inside the constructors you assign a different value to var1. There is nothing special about static variables when it comes to assigning values. You can assign and it will get a new value just like any other variable.