Should I delete a pointer to an int in a class's deconstructor (in C++)? -


this question has answer here:

i'm having hard time understanding pointers, please forgive me ambiguity question may have. yes, i'm asking more 1 question, 1 in title far important, though. other questions questions appreciate answering. lets have class looks this:

class myclass { public:     myclass();     ~myclass(); private:     struct node     {         node * next;     }     node * head;     int myint; } 

now, i'm aware, 1 call constructors class , struct, deconstructors each. i've been told integers shouldn't deleted in deconstructor, deleting pointer integer. shouldn't deleted in deconstructor this:

myclass::~myclass {     delete head;     delete &myint; } 

if not, why?

if want answer question, read no further. sake of brevity, next question code, , deconstructor above, i've been told wrong (assume part of same file deconstructor above):

myclass::node::~node {     delete next; } 

more specifically, i've been told shouldn't delete nodes unless they've been declared new tag. needless say, find confusing. if can, explain mean , why they're saying true? link explaining equally appreciated, understand i'm new coding, started learning c++ few months ago.

you can use delete on memory allocated dynamically using new.

in case, myint not separate block of dynamically allocated memory. it's member of myclass object, , memory cannot managed separately containing object. when allocate myclass object (either ordinary variable, or dynamically using new myclass), members allocated; when free it, memory reclaimed. don't need delete individual members.


Comments