c++ - How to begin writing a smart pointer? -
i've received task in university, write smart pointer. received skeleton, , need implement needed methods. if i'm right, smart pointer type of pointer counts reference numbers given object (?) and, if counter reaches zero, deletes given object.
this skeleton:
template<class t> class my_pointer { public: my_pointer(); }; class refcounted { /* reference counted types should have public interface defined here */ public: int increfcnt(); int decrefcnt(); }; int main() { my_pointer<refcounted> obj1 = new refcounted(); my_pointer<refcounted> obj2 = obj1; return 0; }
firstly, should line do? refcounted not child of my_pointer, how can possibly instantiate new refcounted object , reference my_pointer object (pointer?)?
my_pointer<refcounted> obj1 = new refcounted();
why isn't there counter in my_pointer class, , why counter in refcounted class? how should start this? i'm not in c++. in advance!
firstly, should line do?
my_pointer<refcounted> obj1 = new refcounted();
that line should initialize my_pointer<refcounted>
smart pointer refcounted
raw pointer passed it.
how can possibly instantiate new refcounted object , reference my_pointer object (pointer?)?
you can using converting constructor. my_pointer
doesn't have constructor accepting t*
, seems have been left define.
why isn't there counter in my_pointer class, , why counter in refcounted class?
you're apparently tasked creating intrusive pointer expects stored object reference counting.
how should start this?
i take glance @ description , interface of boost::intrusive_ptr. don't need quite complete interface assignment, though.
Comments
Post a Comment