Practical application of class destructor

后端 未结 6 1565
孤街浪徒
孤街浪徒 2021-01-06 09:48

I\'m currently trying to learn about classes and constructors/destructors. I understand what the two do, but I\'m having a harder time with the destructors because I can\'t

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-06 09:55

    these have the most power when you have composition of dynamically allocated objects

    say that I am making linkedList structure that holds pointers to linkedListNodes the list will hold the pointers to the first, and depending on singly, or doubly the last element.

    in the destructor I would place the removal of all elements in the list, and therefore the list itself.

    If I do not code the destructor then when the list goes out of scope, or has delete called on it the memory that was allocated for those elements is lost, and cannot be reclaimed by the system (most notably called a memory leak)

提交回复
热议问题