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

前端 未结 8 586
故里飘歌
故里飘歌 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:23

    When you free an object, all of its member variables are automatically freed as well. So, in your case, yes, a, b and c are all freed.

    However, if one of your member variables is a pointer, only the pointer itself is automatically freed, not the object it points to - this is the most common case for having to write your own destructor.

提交回复
热议问题