i created abstract class , created child classes inherit abstract class.
class a{ public: virtual a* clone() const = 0; virtual a* create() const = 0; ~virtual a(){}; // etc. private: a(){}; }; child classes
class b: public a{}; class c: public a{}; i can populate vector these classes using pointer of type , access child classes via polymorphism.
vector<a*> pntr; the problem want each child class deal own memory release, kind of raii. raii doesn't work virtual destructors. there way can this?
however raii doesn't work virtual destructors.
of course does. virtual-ness of destructor doesn't matter. need call it. when raw pointers go out of scope, don't destroy object point to. that's unique_ptr for:
std::vector<std::unique_ptr<a>> pointers; when vector goes out of scope, of unique_ptr<a>'s destroyed, delete underlying raw pointers own, call destructors of b or c (or ...) appropriate , free of memory.
side-note: virtual a() wrong; cannot have virtual constructor.
Comments
Post a Comment