constructor - Why C++ primitive types are not initialized like the rest of types? -
I know that, in C ++, when you type
int i ; You can not make any assumptions about the value, unless you effectively assign a value to it. However, if you type
int i = int (); So you have a guarantee that i will be 0 . So my question is, is it really not an incompatibility in language practice? I mean, if I have defined a class MyClass and MyClass myInstance; I can be assured that the default constructor without the class parameters will be called to start myInstance (and if no one then the compiler fails ), Because how RAII principle goes like this though, it appears that when it comes to primitive types, then resource acquisition is not starting anymore. Why is it like this? I do not think that changing this behavior from C will break any existing code (there is such a code in the world that works on the notion that no assumptions about value can be made One of the variables?), Then the main reason for my mind being demonstrated, for example creating a large array of primitive types; But still, I want to know that there is some official explanation on this. Thank you.
no. This is not inconsistency.
What if your class is defined:
struct MyClass {int x; Float Y; Four * z; }; Then this line does not do that you think it is:
MyClass myInstance; The above value is declared within a function, this is the same:
int x; In the C ++ declaration error within the // function, the types are broadly divided into 3 types POD, non-POD, aggregates - and there is a clear difference between them. Please read about them and their initial rules (there are so many topics on them. Search this site) Read also about this.
Comments
Post a Comment