5,321
edits
(→Memory) |
|||
Line 115: | Line 115: | ||
====Custom Deleter==== | ====Custom Deleter==== | ||
[https://www.bfilipek.com/2016/04/custom-deleters-for-c-smart-pointers.html Custom Deleters] | [https://www.bfilipek.com/2016/04/custom-deleters-for-c-smart-pointers.html Custom Deleters]<br> | ||
When using smart pointers, the default deleter is the <code>delete</code> function but you can also specify your own deleter. | When using smart pointers, the default deleter is the <code>delete</code> function but you can also specify your own deleter. | ||
<syntaxhighlight lang="cpp"> | |||
# Using a functor | |||
struct AVFrameDeleter { | |||
void operator()(AVFrame *p) { av_frame_free(&p); } | |||
}; | |||
std::unique_ptr<AVFrame, AVFrameDeleter> rgb_frame(av_frame_alloc()); | |||
# Using free | |||
std::unique_ptr<void *, decltype(std::free) *> my_buffer(std::malloc(10), std::free); | |||
</syntaxhighlight> | |||
===Casting=== | ===Casting=== |