private overloading of new & delete
Hi
I have been looking at some custom memory management and I am curious about new/delete overloading. I would like to know if one can limit overloaded new & delete operators to a template specific class (meaning all de-/allocations of all types occurring in the class are using the overloads). And also subject the overloads to the template for that class so different class variants use different overloads, while those overloads do not affect any code outside the given class. To give some more clarity se the example “code” below:
Code:
template <class Allocator>
class Dummy {
private:
// the operators should only be used by the class itself.
// assume Allocator is a class supplied with the necessary functions
void *operator new (size_t size) {
return Allocator::allocSomeMemory(size); // call template specific func.
}
void operator delete (void *ptr) {
Allocator::deallocTheMemory(ptr); // call template specific function
}
public:
void foobar() {
int *x = new int; // calls the overloaded new operator.
// please note that the allocated object could be of any type,
// including classes
delete x; // calls the overloaded delete operator
}
};
Unfortunately this code won’t compile and even if it would the overloads would only be called when de-/allocating the Dummy class, which is not desirable. Any help or ideas are highly appreciated.