Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In C++ you're supposed to do this with std::allocator and alternatives. This allows you to change the allocator used by vector<>, map<>, etc. Or you use the placement new syntax you found with operator overloading (yes, you can overload "new MyType()")

You're really not supposed to do it with macros.



To be clear, there are four options you mentioned:

1. Changing the allocator used by the standard library by passing in a special allocator to instantiations of std::vector, std::map, etc.

2. Allocating raw memory elsewhere, and instantiating an object with that memory using the placement syntax for new.

3. Overriding operator new on just a particular class. This does not change the global new; it just means that objects of this class will be allocated with this version of new.

4. Overriding the global definition of new. All objects whose classes do not define their own operator new will be allocated with your own, global new.

More details here: http://en.cppreference.com/w/cpp/memory/new/operator_new


std:allocator and overloading new sound like much better alternatives, thank you.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: