For example, with the following piece of code, we can quickly realize that both X and PTR start with undefined values, but SC obviously calls its default constructor to get a reasonable initialization.
Class someclass{
Public
SomeClass (): Val () {}
Private
int Val;
}
void Foo () {
int x;
int *ptr;
SomeClass SC;
......
}
In C + +, there is some difference between handling the underlying type (int, bool, float, double, pointer type, etc.) with the class type. The underlying type does not exist so-called default constructors for initialization.
In the template programming, you can use the following scheme, so that even for the built-in type, you can get the appropriate initialization, the integer initialization of 0, the pointer type is also initialized to 0.
Template <typename t> void foo () { T x = t (); is zero (or false
Template <typename t> class MyClass { private: T x; Public: MyClass (): X () {was initialized even for built-in types }
Built-in type 0 initialization issues