您的位置:首页 > 其它

"delete this;" in the member function

2010-03-03 13:04 405 查看
在网上看到了一个NB的文章:最好的C++面试题目。

http://blogs.windwardreports.com/davidt/2009/12/the-best-c-interview-question-ever.html

原文:

There is no one magical question that can determine if someone is the right candidate. There isn't even one that will determine if their technical knowledge is up to par. But there are those occasional questions that provide a lot of bang for the buck – simple questions that give you a lot of feedback about the candidate.

If I could ask a C++ programmer just one technical question, this would be it. Why? Because the correct answer feels wrong and the initial answer is generally wrong. So seeing how they work through the explanation is very illustrative of not just their knowledge of C++, but their willingness to not get locked in to their initial answer.

Q: Can you call "delete this;" inside a member function?

The instinctual answer of almost everyone is No. Not only no but there is a visceral emotional response from most that doing so would be like crossing the beams in Ghostbusters – very bad news. Ok, so next step, regardless of if they answer Yes or No is to ask "Why?"

This is where you really learn a lot. Even people who answer Yes (and they are few and far between), most of them have trouble explaining what is going on. I have seen many, over 5 – 10 very painful minutes, sketch out most of what is going on, and yet still say that you cannot do it. These are people you do not want to hire under any circumstances because they cannot think outside their initial preconceptions.

I have also had many with some of the most outlandish understandings of what is actually going on in the computer. Many people think when you call the delete that the memory the code is running on is garbage collected. Not the data – the code! Some think the v-table disappears on calling a delete (it doesn't as it's tied to the class) – and they think the v-table is needed for the return from the method.

Ok, so you then have one of the few who has worked through the above, got it figured out, and you're feeling good. We then have a follow-up question that will drop out half of the remaining candidates – "What can you do after calling delete this?"

And you will get answers that are all over the board from "anything" to "nothing." I've had many ask me if local variables are consider part of the object memory – so at least those people are thinking it through even if they don't fully understand what belongs to an object.

So there you go – a great interview question until it is over-used enough that everyone knows about it (like the manhole cover question).

Update: This has received a large number of comments (especially on reddit). I want to stress one key point - I don't ask this because I expect most people to get the initial answer right. If they did it would not be that useful a question. I ask to see if they can work their way to the correct answer and how they do so. As to the question of why would you ever do this - it is very useful in message based systems and when threads are exiting.

Ps – If you have a great "just one question" – please post it in a comment below.

针对best,褒贬不一。我特意写了一个程序来做测试:

#include <iostream>
using namespace std;

class Dog
{
public:
Dog()
{
std::cout << "Dog constructor" << endl;
}

~Dog()
{
std::cout << "Dog deconstructor" << endl;
}

void release()
{
std::cout << "Dog::release()" << endl;
delete this;
}

void print()
{
std::cout << "Dog::print()" << endl;
}

private:
int m_i;
};

int main()
{
Dog *pDog2 = new Dog();
std::cout << "----------->Dog2<------------------" << endl;
pDog2->print();
pDog2->release();

Dog dog1 = Dog();
std::cout << "----------->Dog1<------------------" << endl;
dog1.print();
dog1.release();

return 1;
}


执行结果:

ewuming/tmp> ./c.out
Dog constructor
----------->Dog2<------------------
Dog::print()
Dog::release()
Dog deconstructor
Dog constructor
----------->Dog1<------------------
Dog::print()
Dog::release()
Dog deconstructor
Segmentation fault

分析:

通常情况下,类的内存可能出现在两个地方:stack和heap。

stack中,保存的是整个class的所有信息,在这个function执行return的时候,执行析构函数。

heap中,class的所有信息保存在heap中,只有在delete的时候才执行析构函数。

根据执行结果,heap中的class,member function可以有delete this;stack中的class,member function中如果有delete this,则出现了segementation fault。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐