Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 242 times

Related Categories

[C++] Automatically Delete Memory for Static Class Members - Descriptions & Code

Descriptions & Code

===========<   Descriptions   >==========

-> VSC++ 2005 - Win32 Console App
Problem : When you have a static pointer variable (like: static char*st_ptr) in your class, that you want to allocate some memory to that (during runtime with operator new), then at the end of program you want to delete that memory. How can you delete that at the end of program ?! Just using some way to access that memory at the end of main function, then delete it? But when you want to give your class to a customer, you should say to him : “Delete some of memory at the end of your main function, Thanks!”, terrible! really impractical! Then you should do that in some way in your class .
 
Solution : Benefit of destructors , they’re called just at the end of something!
 
My Implementation is : define a static struct in your class (private or public), then add all of your static variables as non-static members of that struct, then all of those variables will become static variable members of your class (except, just use struct name in addition to class name, for accessing them). You can initialize those vars in the struct constructor, and don’t need to define them in the file scope (you just declare the struct itself : ‘class1::statics class1::st1;’ ).
 
Other Benefits : wrap up all the static vars in a struct, facilitate initializing static vars.
 
NOTE : 1.The constructor of structure will be called at the beginning of program and destructor  will be called at the end of program (even if there is not exist any instances of the class, i.e., you don’t declared any variable of class type, since the structure will exist (constructed) from where it’s declared [class1::statics class1::st1;] to the end of program).
2.In VC++6 : destructor of structure may not work as you expect, then compile in VC++05.

===========<   Code    >==========

#include <iostream>
using namespace std;
 
#include <cstring>
 
class class1
      {
 
      public:
            static struct statics
            {
                 
                  int   st_int;
                  char* st_ptr;
 
                  statics()
                  {
                        st_int = 35;
                        st_ptr = new char[20];
                        strcpy( st_ptr, "struct_String");
 
                  }
 
                  ~statics()
                  {
                        delete st_ptr;
                  }
            }st1;
      };
 
 
class1::statics class1::st1;
 
 
int main()
{
     
      class1 cl1;
      cl1.st1.st_int = 43;
      strcpy( cl1.st1.st_ptr, "main_String"); // don't use 'cl1.st1.st_ptr = "String"', because it will get a const-pointer, then when you want to delete that and you can't!
 
      class1 cl2;
 
      cout << cl2.st1.st_ptr << endl << cl2.st1.st_int << endl; // prove that the constructor of struct will be called just one time
 
      return 0;
}

Comments