std::unique_ptr<T,Deleter>::get_deleter
来自cppreference.com
< cpp | memory | unique ptr
Deleter& get_deleter() noexcept; |
(C++11 起) | |
const Deleter& get_deleter() const noexcept; |
(C++11 起) | |
返回会用于析构被管理对象的删除器对象。
参数
(无)
返回值
存储的删除器对象。
示例
运行此代码
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo...\n"; } ~Foo() { std::cout << "~Foo...\n"; } }; struct D { void bar() { std::cout << "Call deleter D::bar()...\n"; } void operator()(Foo* p) const { std::cout << "Call delete for Foo object...\n"; delete p; } }; int main() { std::unique_ptr<Foo, D> up(new Foo(), D()); D& del = up.get_deleter(); del.bar(); }
输出:
Foo... Call deleter D::bar()... Call delete for Foo object... ~Foo...