0

According to what I read about classes and in particular about destructors, in the end of the running, the destructor called automatic. If so, in which cases I will needed to call to destructor myself (or that it's will not be).

Software_t
  • 576
  • 1
  • 4
  • 13
  • Do you mean directly invoking the destructor? There's only one case where you need to do that, when you constructed something via placement `new` – UnholySheep Mar 13 '18 at 09:35
  • 3
    Wanting to use placement new, that's the only case. – Hatted Rooster Mar 13 '18 at 09:35
  • The only reason to ever want to explicitly call a destructor is to witness [object slicing](http://www.gotw.ca/gotw/022.htm) for real. – IInspectable Mar 13 '18 at 09:41
  • @SombreroChicken And when I will want to allocate (by a `new`) a variable in type of the class? – Software_t Mar 13 '18 at 09:41
  • you want to use `new` if you like pain and trouble. When you need to call `new` is a different question :P – 463035818_is_not_an_ai Mar 13 '18 at 09:42
  • 2
    @Jor.Mal: You don't. Use objects with automatic storage duration, and C++ containers. There is no place for `new` or `delete` in C++ today. – IInspectable Mar 13 '18 at 09:42
  • 1
    btw dont confuse calling `delete` with calling the destructor. Those are two related, though different things – 463035818_is_not_an_ai Mar 13 '18 at 09:43
  • @user463035818 If I allocated a variable in type of the class (by `new`)so I need to call destructor manually and in the destructor will be the `delete`, not? – Software_t Mar 13 '18 at 09:45
  • its not the complete truth, but close enough: You never need to call the destructor directly! – 463035818_is_not_an_ai Mar 13 '18 at 09:46
  • @Jor.Mal You should destroy the object with `delete` - this will first use the destructor and then free the memory the object occupied. There's a fair chance that you will never encounter a situation where explicit use of the destructor is a good thing. – molbdnilo Mar 13 '18 at 09:55

0 Answers0