Since boost::/std::shared_ptr
have the advantage of type-erasing their deleter, you can do nice things like
#include <memory>
typedef std::shared_ptr<void> gc_ptr;
int main(){
gc_ptr p1 = new int(42);
gc_ptr p2 = new float(3.14159);
gc_ptr p3 = new char('o');
}
And this will correctly delete all pointer thanks to the correct deleter being saved.
If you ensure that every implementation of your interface always gets created with shared_ptr<Interface>
(or make_shared<Interface>
), do you actually need a virtual
destructor? I would declare it virtual
anyways, but I just want to know, since shared_ptr
will always delete the type it was initialized with (unless another custom deleter is given).
shared_ptr
does it, but whether you should use a virtual destructor knowing thatshared_ptr
does that magic. – Oleumshared_ptr
instances that could point to a derived type, then you're almost certainly usingvirtual
member functions anyway, right? Otherwise, what's the point? So in that case, making the destructor virtual doesn't cost anything... except maybe a little extra typing :) – Flydelete(void*)
. I don't see the connection between a custom deleter and deletion-through-base-pointer. – Vassauxvoid*
, but cast it to the right type,int
,float
andchar
respectively. – Kikelia