Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

As referenced in this site... http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10 But i did not find the reason, why we should explicitly call the desturctor?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
385 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can think of it as a call to delete, but since you used placement new, you don't want to use delete, as that would attempt to free the memory. If you wanted it to be called automatically, you could use RAII:

// Could use a templated version, or find an existing impl somewhere:
void destroy_fred(Fred* f) {
   f->~Fred();
}

void someCode()
{
   char memory[sizeof(Fred)];
   void* p = memory;
   boost::shared_ptr<Fred> f(new(p) Fred(), destroy_fred);

   // ...

   // No need for an explicit destructor, cleaned up even during an exception
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...