class base { public: f(void) { delete this; } }; class derived : public base { }; int main() { derived D = new derived(); D->f(); } which object is deleted?
d*n
3 楼
I thought "this" is a constant pointer. so, can this be deleted? maybe you will get a compiler error.
s*e
4 楼
first, in your main function, you should make the derived object with new operator, such as: derived * pD = new derived(); pD->f(); and then, the base part of the object is deleted. And that's why you should put a virtual destructor in base, in that case the whole object is deleted.
【在 b***y 的大作中提到】 : class base : { : public: : f(void) { delete this; } : }; : class derived : public base : { : }; : int main() : {