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

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

    Class members are a part of the class' memory structure.

    So when you free that memory, the members are freed with it.

    NOTE:
    If you have pointers they are destroyed too, BUT the memory they point at isn't destroyed.

    More about class memory consumption:

    C++ Classes

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题