Visual Studio 2013 C++ class properties initialization problems -
Can anyone tell me what's going on with this code?
square point {private: int x; Int y; Public: point () {} zero print () {cout & lt; & Lt; X & LT; & Lt; "" & Lt; & Lt; Y & lt; & Lt; Endl; }}; Int main () {Point P; P.print (); Return 0; } If I run this code then the output is -858993460 -858993460 The witch is normal for me, this is a waste because I have not started my 2 properties.
Here's the weird thing ...
square point {private: int x; Int y; Int * buf; Public: point () {} zero print () {cout & lt; & Lt; X & LT; & Lt; "" & Lt; & Lt; Y & lt; & Lt; "" & Lt; & Lt; Buf & lt; & Lt; Endl; }}; Int main () {Point P; P.print (); Return 0; } Now, I put int * buf as a member of a new class and when I run this code, all of my properties start at zero. Production is 0000000000. I am using Visual Studio 2013 and I do not think this behavior is taking place in Visual Studio 2010.
Can anyone explain the logic behind it?
Members of your class are never started at zero, you have to do it in your constructor
square point {private: int x; Int y; Public: point (): x (0), y (0) {} zero print () {cout & lt; & Lt; X & LT; & Lt; "" & Lt; & Lt; Y & lt; & Lt; Endl; }}; If they are zero in your second example, then this is a pure coincidence
Comments
Post a Comment