Class Destruct
-
Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter
-
Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter
if you "new" an object, then the destructor is only called when you "delete" it. if you allocate the object on the stack, the destructor is called when the object goes out of scope. one or the other (or you can explicitly call the destructor, but that's something you can't really do without knowing you're doing it) -c
//The Mandelbrot set
o(int O){putchar(O);}main(){float l[8],O,I=.05;char _;for(l[6]=15;l[6]<':';o
(10),l[5]=-'$'*I+l[6]++*I)for(l[7]=-5;l[7]<'@';l[4]=-'('*I+l[7]++*I,o(_?'?':':'))for
(*l=O=0,_=1;++_&&((l[2]=*l**l)+(l[3]=O*O)<4);O=*l*O+l[5]+O**l,*l=l[2]-l[3]+l[4]);} -
Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter
If you store a pointer to class A as member variable in class B, and class B is designed to delete the A object in its own destructor, you should reset the pointer to NULL when you manually delete object A. This will make it clear:
B:: ~B()
{
// Free A
if (m_ptrA != NULL) {
delete m_ptrA;
}
}B::someFunction()
{
m_ptrA = new A();
...;
delete m_ptrA;
m_ptrA = NULL; // <--- Reset pointer after deleting object
}/ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com
-
Hey Guys If i use an instance of a class in a funtion then deallocate it is it deallocated again when the function drops out. I only ask cos i keep getting a close handle again on my file access class at the end of my function even though i have already destructed the instance and haven't created another. Peter
No. But if you haven't created your own copy c'tor and assignment operator a HANDLE wrapper class might use the compiler generated versions and then two object might try to release the same resoucrce when they are destructed.