overloading global new operator

Started by
3 comments, last by snk_kid 19 years, 5 months ago
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
Advertisement
::new
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);}
daerid@gmail.com
still recursive
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);}

This topic is closed to new replies.

Advertisement