Skip to main content
GameDev.net gamedev.net
🔒 Locked

overloading global new operator

Started by Silly_con Nov 9, 2004 at 5:01 PM 3 replies 800+ views
Original Post
Silly_con
Silly_con
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
daerid
daerid
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
Silly_con
Silly_con
still recursive
snk_kid
snk_kid
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);}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.