-
Static member of a class
Hello everyone.
I have a class but I want to create a static member of the class, so the objects can exchange data throught it. The member must be a dinamic char*, so I can pass strings through it, but the problem is how to 'delete' the dinamic memory, which I have alocated when there are no more objects?
The only one sollution that I could think out was to have another one static variable, which counts the instances of the class and when the counter reaches 0, then I delete the memory in the destructor of the class.
I wonder is any other way to manage static variables? And is there any way to do it in the end of the program no matter if there are 'live' objects?
-
A string would be better because it would clean itself up... or perhaps you could use a smart pointer. Alternately, to do it with a basic character pointer as you are attempting, you would also need a static counter data member that would be set to 0 initially and then incremented each time a constructor was called and also decremented every time the destructor was called. If, when in the destructor after you've just decremented this value, you check the value and it is 0 then you know that there are no more instances of this object so you can then delete the memory at that point.
-