Deleting derived classes in std::unique_ptr<Base> containers

后端 未结 2 634
我寻月下人不归
我寻月下人不归 2021-01-07 05:05

I\'m a little confused. Basically, I\'ve got 2 different resource managers (AudioLibrary and VideoLibrary) that both inherit from a shared BaseLibrary class. This base class

2条回答
  •  半阙折子戏
    2021-01-07 05:17

    The default deleter for unique_ptr is the aptly named default_delete. This is a stateless functor that calls delete on its T * argument.

    If you want the correct destructor to be called when a unique_ptr to a base class is destructed, you must either use a virtual destructor, or capture the derived type in a deleter.

    You can do this quite easily using a function pointer deleter and captureless lambda:

    std::unique_ptr pb
        = std::unique_ptr(new D,
            [](B *p){ delete static_cast(p); });
    

    Of course, this means that you need to add the template argument for your deleter to all uses of unique_ptr. Encapsulating this in another class might be more elegant.

    An alternative to this is to use shared_ptr, as that does capture the derived type, as long as you create the derived shared_ptr using std::shared_ptr(...) or, preferably, std::make_shared(...).

提交回复
热议问题