Originally Posted by
vb.bajpai
Naturally the usual caveats apply in cases where your this pointer is a pointer to a base class when you don't have a virtual destructor. - how can a this pointer point to base class ? and if such is a case, is this too polymorphic?
for example,
Code:
class Base
{
public:
~Base() {}
void DeleteMe() { delete this; }
};
class Derived : public Base
{
public:
~Derived() {}
};
int main()
{
Base *pB = new Derived();
pB->DeleteMe(); // this is not good.
}
In this case, ~Derived() is never called. to fix this you should make ~Base() virtual.
Originally Posted by
MacGyver
Frankly, I don't think you should be doing delete this anyway.... There's just too much of a risk to break the code somehow. If you're confused about any aspect, I would recommend you don't do it.
In this specific instance, agreed. No offense, vb.bajpai, but I don't quite think you're ready for this yet.
That said, as long as you control creation of the object (.i.e. with a private constructor and a static Create() member that ensures it's created with new) 'delete this' is a perfectly valid idiom.