Smart pointers have appeared in the new standard, so now what to write in destructors?
3 answers
Well, theoretically, now you can write nothing in destructors. In smart pointers, you can specify a functional object that is responsible for freeing resources. That is, you can not be limited to the release of memory. Using smart pointers, you can close connections to the database, open files, call some functions like CloseHandle from the Win API. For example:
#include <iostream> #include <string> #include <memory> //Представим что это какой-то ресурс, который нельзя просто создать или удалить struct Res{ std::string name; Res(const std::string &name): name(name) {} }; //Функция для создания Res* getRes(const std::string &name){ std::cout << "Res{" << name << "} created" << std::endl; return new Res(name); } //Функция для удаления void deleteRes(Res *res){ std::cout << "Res{" << res->name << "} deleted" << std::endl; delete res; } int main(){ std::unique_ptr<Res, void(*)(Res*)> pRes1(getRes("unique_ptr"), &deleteRes); //Res{unique_ptr} created std::shared_ptr<Res> pRes2(getRes("shared_ptr"), &deleteRes); //Res{shared_ptr} created //Res{shared_ptr} deleted //Res{unique_ptr} deleted } If you began to use an object of a certain class with a smart wrapper, now you do not need to call delete for this object.
If you had a situation like
class Foo { Bar* bar; Foo() { bar = new Bar(); } ~Foo() { delete bar; } }; and you decide to replace Bar* with std::shared_ptr<Bar> , you can either delete the Foo destructor or leave it empty, because smart pointers were created to destroy it (as happens when the Foo class is deleted, since bar now not a pointer, but an independent object of another class) the resource that they own was deleted.
- I understand how to use smart pointers, I wanted to know what other memory release implementations may be present in the destructor that are not related to pointers - Sergey
DbConnectionClose,CloseHandle,DbConnectionCloseand so on - ixSci- I will read. thank. - Sergey
- one@ixSci when setting up a custom remover, by the way, they can also be successfully used as part of a smart point. - αλεχολυτ
According to the "rule of zero" (Rule of Zero, C ++ Reference , Core Guidelines , original article ), the destructor should be written only if the class is responsible for the ownership of the object. At the same time, according to the principle of one responsibility ( SRP ), a class should not be responsible for anything else.
In other words, if a class is not a smart pointer / handle / etc, then it does not need a destructor. In this case, for example, smart handles can often be done through unique_ptr , passing it the necessary deleter ( example ).
Of course, if you need a virtual or secure destructor, you must declare it (with =default; ).
c++14.c++17on the way. And smart pointers inc++03were, for example,std::auto_ptr. - αλεχολυτauto_ptris a crazy pointer - ixSciумных. - αλεχολυτauto_ptris a miscarriage, which is not clear at all why they added. - ixSci