invoking destructor from within instance method

F

Fred

Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFunction() {
SomeOtherClass:someFunction(this);
// No other references to 'this'
// after this point
return true;
}

SomeOtherClass::someFunction(MyClass m) {
delete m;
}
 
J

Joshua

Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFunction() {
SomeOtherClass:someFunction(this);
// No other references to 'this'
// after this point
return true;

}

SomeOtherClass::someFunction(MyClass m) {
delete m;

}


Unsafe.

Myclass m;
m.someFunction(); // delete variable on stack!
 
J

James Kanze

Is it legal to invoke my destructor from within an instance function?

Of course not. It's probably the most common case, in fact.
For example,
bool MyClass::someFunction() {
SomeOtherClass:someFunction(this);
// No other references to 'this'
// after this point
return true;
}
SomeOtherClass::someFunction(MyClass m) {

The parameter should be a pointer, of course.
delete m;
}

I'm not too sure I like the added indirection. Most of the
time (something slightly over half), deletion is done by "delete
this".
 
J

James Kanze


No more dangerous than anything else.
Myclass m;
m.someFunction(); // delete variable on stack!

And how does the fact that someFunction() is a member change
anything. If the object has value semantics, no one should
delete it, since it shouldn't be allocated dynamically. And if
it has identity, all instances will be allocated dynamically,
and deletion will be in response to some external event. If the
event is processed by the object itself, then the object
deletes itself; if the event is processed by some other object,
then that object deletes the object. Obviously, there are other
kinds of objects as well, but these two represent well over 90%
of the objects in a typical simple application.
 
S

sumsin

Is it legal to invoke my destructor from within an instance function?

For example,
bool MyClass::someFunction() {
SomeOtherClass:someFunction(this);
// No other references to 'this'
// after this point
return true;

}

SomeOtherClass::someFunction(MyClass m) {
delete m;

}

It should be like:
SomeOtherClass::someFunction(MyClass *m) {
delete m;
}

because you are passing a pointer.

Though its legal but its not safe.

Functions are made to call. Though there is no reference of 'this'
pointer after deleting it in some other class's member function, but
the caller who is calling the 'someFunction' of 'MyClass' (of course
through some MyClass variable) doesn't know about this deletion and
think what will happen if he try to access some other property of
MyClass?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top