C++ Deleting Static Data

后端 未结 5 1581
难免孤独
难免孤独 2021-02-12 11:47

If I have a class that contains private static data allocated on the heap that never changes, when, if at all, should I delete it?

As I understand it, a class itself is

5条回答
  •  我在风中等你
    2021-02-12 12:17

    static data means, it persists the entire duration of the program.

    However, if you use static in pointer as:

    static A *pA = new A();
    

    then you can delete this, by writing delete pA. But that doesn't invalidate my first statement. Because the object which is being pointed to by the static pointer is not static. Its the pointer which is static, not the object which is being pointed to by the pointer.

提交回复
热议问题