c++ - Initializiation confusion -


not sure of appropriate title, stems discussion:

do parentheses after type name make difference new?

on visual studio 2008, when run following code:

struct stan {     float man; };  int main() {     stan *s1 = new stan;     stan *s2 = new stan();  } 

examining locals, s1 has uninitialized float random value. s2 value initialized 0.

however, if add string data member, float in both instances uninitialized.

struct stan     {             std::string str;         float man;     };  

however, string in both instances initialized. tried adding other non-pod classes instead of string, latter case occurs if add string data member. gather adding string still keeps pod class? if wasn't pod class, should have value initialized regardless of parenthesis, right? ideas why floats(and other primitive data types matter) aren't initialized when add string data member?

adding string stops struct being pod class because pod class must aggregate class no members of type non-pod-struct , std::string has (amongst other things) user-declared constructors makes non-pod-struct.

this known bug/feature of visual studio 2008. doesn't support c++03 value initialization non-pod types such structure in second example.

with struct in second example should happen float not initialized new stan 0 initialized in new stan().

types user declared default constructor initialized calling constructor in cases, happens correctly.

see here , here.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -