C++ Destructors with Vectors, Pointers,

后端 未结 7 2025
清酒与你
清酒与你 2020-12-15 02:57

As far as I know, I should destroy in destructors everything I created with new and close opened filestreams and other streams. However, I have some doubts abou

相关标签:
7条回答
  • 2020-12-15 03:39

    If I have something like std::vector what happens when the vector destructor is called?

    It depends.

    If you have a vector of values std::vector <MyClass>, then the destructor of the vector calls the destructor for every instance of MyClass in the vector.

    If you have a vector of pointers std::vector <MyClass*>, then you're responsible for deleting the instances of MyClass.

    What happens if I have a pointer to another class inside a class

    ClassB instance would remain in memory. Possible ways to have ClassA destructor to make the job for you are to make B an instance member or a smart pointer.

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