Is there a way to define a macro (or something similar) that would allow objects to be allocated on the stack or on the heap, cleanly?
eg. Current code:
A a; a.someFunc();
The simplest suggestion might be the following, but as you can see below, it’s not very clean to maintain 2 sets of code.
#ifdef USE_STACK A a; a.someFunc(); #elseif USE_HEAP A* a = new A(); a->someFunc(); #endif
I am looking for a design pattern / proxy class that can be use to compile the code either way, depending on the needs of our customer.
Edit: The code is used to build a library for embedded device / (embedded) Linux / Windows Mobile. Most customers want stack based allocation only. A few others have asked to trade stack for heap.
Thanks, Charles
EDIT: improved to allow calling of wrapped member functions through
operator->Expanding on Manuel’s answer to make it more complete, try this: