Friends, what's happend if i not instance the initial value of variables in constructor, boolean and numeric values, i wanna start the values with zero and false, so i need to implement it in the constructor or no?
like this:
Class::Class(unsigned long v):variableToChange(v){
or this
Class::Class(unsigned long v):variableToChange(v),boolVar(false),intVar(0){
Because in the second case, the binary size is greater.
wizarcl:
Friends, what's happend if i not instance the initial value of variables in constructor, boolean and numeric values, i wanna start the values with zero and false, so i need to implement it in the constructor or no?
like this:
Class::Class(unsigned long v):variableToChange(v){
or this
Class::Class(unsigned long v):variableToChange(v),boolVar(false),intVar(0){
Because in the second case, the binary size is greater.
The second case is probably because there are additional data members? If you don't use those additional variables anywhere else in your program then the compiler probably optimises the code and doesn't include them in the binary at all.
If you don't init the values they will have garbage values, i.e. what ever was in the memory address of those variables. The second code is bigger because the compiler adds code to init the variables with the false & 0 values you defined
wizarcl:
I use all this variables, but in the second case, the binary size is bigger, but it is best way to program?
If you don't init the values they will have garbage values, i.e. what ever was in the memory address of those variables..
Yes, the constructor is the best place to initialise member variables, it's what it's there for. If you don't initialise in the constructor then those variables have an unknown value until you initialise/set them elsewhere in your application.