c++ - determining if templated type is dynamic -


i writing template of working class, , might dumb question, if have template structure (linked list) hold possibly pointers objects how know being deleted, or pointers in first place?

for example: linkedlist used in 2 ways in program

a pointer object of class thing placed inside node inside linkedlist

an enum placed inside node inside linkedlist

i know nodes being deleted, how know thing in node pointer can deleted well, , not null referenced object?

you can specialize node based on type of object, , pointer specialization, create destructor node-type allocates , deletes pointer managed node.

for instance:

//general node type non-pointer types template<typename t> struct linked_list_node {     t data;     linked_list_node<t>* next;      linked_list_node(const t& d): data(d), next(null) {}     ~linked_list_node() {}  };  //specialized version pointer types template<typename t> struct linked_list_node<t*> {     typedef void (*deleter)(t*);      t* data;     linked_list_node<t>* next;     deleter d_func;  //custom function reclaiming pointer-type      linked_list_node(const t& d): data(new t(d)), next(null), d_func(null) {}      linked_list_node(const t& d, deleter func): data(new t(d)),                                                  next(null), d_func(func) {}     ~linked_list_node()      {         if(d_func)             d_func(data);  //execute custom function reclaiming pointer-type         else             delete data;     } }; 

you can instantiate different versions passing correct template argument when creating instance of linked_list_node type. instance,

linked_list_node<myptr*> node(fooptr); //creates specialized ptr version linked_list_node<myenum> node(fooenum); //creates non-ptr version of node 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -