c++ - Building an Object System Around shared_ptr -
i using shared_ptr
garbage collection toy language working on compiles c++. objects derive common base class above there strings , numbers there vectors , maps. on c++ side passed wrapped in shared_ptr
s containers hold shared_ptr
when destroyed content destroyed too. scheme works feels bit weird in containers base objects holding shared_ptr
s. there flaw design? if yes alternative hierarchy around approach?
here's how i'd set up:
namespace toylang { class object; // main handle type; use object references // replace boost::intrusive_ptr or similar if inefficient typedef std::shared_ptr<object> obj; class object { // whatever }; class number : public object { int x; // etc }; class array : public object { std::vector<obj> a; // etc }
note toylang arrays in scheme vectors of pointers, giving language reference semantics. in fact quite common in dynamic languages: lisp, python, , others work that. long don't have circular references, shared_ptr
's reference counting give proper garbage collection.
Comments
Post a Comment