• Advertisement
Sign in to follow this  

overloading global new operator

This topic is 4916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can you overload the new global operator, using standart new operator inside the overloaded operator function body, without entering in an infinite recursive function ? thnx

Share this post


Link to post
Share on other sites
Advertisement

typedef void* (*global_new_ptr)(size_t);

global_new_ptr global_new = ::operator new;

void* operator new(size_t s)
{
// do some book keeping
// allocate memory
return (*global_new)(s);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Silly_con
still recursive


Well of course, thats not overloading that is just redefining it (well trying to and going into an infinite recusion) you wont something like this:


#include <new>
#include <iostream>

struct foo_t {};

extern const foo_t foo; //no need to define it.

inline void* operator new(size_t n, const foo_t&) throw() {
std::cout << "MY NEW\n";
return ::operator new(n, std::nothrow);
}

inline void operator delete(void* ptr, const foo_t&) throw() {
std::cout << "MY DELETE\n";
::operator delete(ptr, std::nothrow);
}

int main() {


int* int_ptr = new(foo) int(30);

std::cout << *int_ptr << std::endl;

operator delete(int_ptr, foo);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement