Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How can i overload the new operator in template class? (http://www.go4expert.com/forums/i-overload-operator-template-class-t28991/)

ssedbe 1Sep2012 10:00

How can i overload the new operator in template class?
 
I tried to overload the new operator in the template class below to use malloc instead of new, but I wasn't successful.

Code:

template< int SIZE >
class MemPoolT : public MemPool
{
public:
MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
~MemPoolT()
{
for( int i=0; i<blockPtrs.Size(); ++i ) {
delete blockPtrs[i];
}
}
virtual void* Alloc()
{
if ( !root ) {
    // Need a new block.
    Block* block = new Block();
    blockPtrs.Push( block );
    for( int i=0; i<COUNT-1; ++i ) {
        block->chunk[i].next = &block->chunk[i+1];
    }
    block->chunk[COUNT-1].next = 0;
    root = block->chunk;
}
void* result = root;
root = root->next;
++currentAllocs;
if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
nAllocs++;
return result;
}
private:
enum { COUNT = 1024/SIZE };
union Chunk {
    Chunk* next;
    char mem[SIZE];
};
struct Block {
    Chunk chunk[COUNT];
};
Chunk* root;
int currentAllocs;
int nAllocs;
int maxAllocs;
};

How can I do this?


All times are GMT +5.5. The time now is 20:29.