-
let s say you have class that allocates some memory when a object is created constructor is in the game when an object is created and deconstructor is called when you finished your job with the object (maybe you want to delete the memory that the object allocates)
-
>>ACTUALLY, I'm going to throw a whole bomb into the mix!
There is a case where constructors and destructurs would not be called!
try this:
code:--------------------------------------------------------------------------------
MyClass *test;
test = (MyClass*)new char[sizeof(MyClass)];
test->SomeMemberFunction();
delete [] (char *)test;
--------------------------------------------------------------------------------
this is 100% legal and the constructors and destructors do not get called! what do you think about that? <<
This is valid. :) You may have not noticed it but you already have initialized the private data within the pointer class when you allocated memory for it via the 'new' operator, just like you would do when you allocate memory for a structure.
Remember, a class is a structure too. :cool:
-
das only true if your allocation was supposed to be to zero. Anyway, I don't recall "new" zero initting its memory. I've always done that explicitly with a memset.
-
>>das only true if your allocation was supposed to be to zero. Anyway, I don't recall "new" zero initting its memory. I've always done that explicitly with a memset.<<
Yah you're right about that. :) There would be garbage in it if not set to zero. But that would be a good start since you have already allocated memory for it.