Question on overloading the operator delete...

This is a discussion on Question on overloading the operator delete... within the C++ Programming forums, part of the General Programming Boards category; I'm curious to know how i would get around the static member error. Code: #include <stdlib.h> struct image { short ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    Question on overloading the operator delete...

    I'm curious to know how i would get around the static member error.

    Code:
    #include <stdlib.h>
    
    
    struct image
    {     short count;
          unsigned int * num;
          unsigned int ** nums;
          void Init( short number )
          {
                num  = NULL;
                nums = NULL;
                count = number;
                if( number == 1)
                num = (unsigned int *)malloc( sizeof(unsigned int));
                else if ( number > 1 )
                {
                    nums = (unsigned int **)malloc( sizeof(unsigned int) * number);
                    int i;
                    for( i = 0; i < count; ++i)
                    {
                         nums[count] = NULL;
                    }
                }
          }
          
          void operator delete (void *p)
          {
                image * q = (image * )p;
               if( q->count == 1 )
               { free(q->num); num = NULL ; }
               else if( q->count > 1 )
               {
                    int i = 0;
                    for( i = 0; i < q->count; ++i)
                    {
                         if( q->nums[q->count] != NULL )
                         free( q->nums[q->count] );
                    }
                    free( q->nums );
               }
               free(p);
               p = NULL;
          }
          
    };
          
          int main(void)
          {
              image * something;
              something->Init( 1 );
              delete something;
              
              return 0;
          }
    I was trying to make a image struct to hold either 1 array of unsigned integers or an array of images. I wanted to push each structure into a class hash table I built a while back, but I had an issue using delete in my hash table and with the lost of memory from the array of unsigned int. So i thought about using a overloaded method for delete to avoid the problem, and not to reconstruct my hash table just around this one issue.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    It sounds like you want to define the destructor rather than overload operator delete. You should also define (or declare as private) the copy constructor and copy assignment operator, and possibly also the default constructor. You might want to change the Init member function into a constructor that takes a short. This constructor might be declared explicit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    { free(q->num); q->num = NULL ; }
    operator delete is always static, even if you don't declare it so.

    But besides laserlight's remarks, does one image have to be a special case (perhaps this is a minor optimization, but perhaps it isn't), and why would a struct called image store multiple images in the first place (instead of it storing data of one image, and an array of image objects used for multiple images)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Thanks for the advice, I think i will go with laserlight's advice first just to try it out, but if not I'll just go with changing the hash table template to handle both array of unsigned int and array of unsigned int arrays ( when I take images from one image source and extract to put into those arrays).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading question
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2007, 12:26 PM
  2. constructor overloading question
    By rozner in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2006, 09:54 PM
  3. overloading question
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 08:12 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Operator Overloading newbie question
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21