If I delete a class, are its member variables automatically deleted?

前端 未结 8 581
故里飘歌
故里飘歌 2020-12-13 06:28

I have been researching, and nothing relevant has come up, so I came here.

I am trying to avoid memory leaks, so I am wondering:

Say I have class MyCla

8条回答
  •  自闭症患者
    2020-12-13 07:20

    delete will reclaim the memory that your object contains. If your type maintains pointers to dynamically allocated memory then you will need to clean those up inside of your destructor.

    As for your specific quesiton:

    after I call delete mc, will a, b, and all the contents of c be deleted as well? Or will I have to do that explicitly in the destructor of MyClass?

    They will be cleaned up for you as they were not allocated dynamically.

提交回复
热议问题