operator delete
-
Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!
-
Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!
Eikthrynir wrote:
But why does the destructor get called when I delete pObj;
Because that's what happens when you use
delete
on an object.Eikthrynir wrote:
because the operator delete function gets a void * as parameter.
Destructors don't take arguments.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!
Eikthrynir wrote:
But why does the destructor get called when I delete pObj;
The compiler does the job: it sees that you are calling delete on a MyClass object, so it supposes that it is a MyClass object and calls its destructor. If you delete a void pointer, the compiler only sees a void pointer.
Cédric Moonen Software developer
Charting control [v1.4] -
Eikthrynir wrote:
But why does the destructor get called when I delete pObj;
Because that's what happens when you use
delete
on an object.Eikthrynir wrote:
because the operator delete function gets a void * as parameter.
Destructors don't take arguments.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Wow, I can barely read his post but yours is rated a '1'? Well, I'll just have to do something about that. :-\
led mike
We're number one! We're number one!
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Eikthrynir wrote:
But why does the destructor get called when I delete pObj;
The compiler does the job: it sees that you are calling delete on a MyClass object, so it supposes that it is a MyClass object and calls its destructor. If you delete a void pointer, the compiler only sees a void pointer.
Cédric Moonen Software developer
Charting control [v1.4]