Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 182509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:57:07+00:00 2026-05-11T14:57:07+00:00

Is there a way to define a macro (or something similar) that would allow

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:57:08+00:00Added an answer on May 11, 2026 at 2:57 pm

    EDIT: improved to allow calling of wrapped member functions through operator->

    Expanding on Manuel’s answer to make it more complete, try this:

    #include <iostream>  #define USE_STACK  template <class T> class HeapWrapper { #ifdef USE_STACK     T obj_; #else     T *obj_; #endif public: #ifdef USE_STACK     HeapWrapper() : obj_() {}      template <class A1>     HeapWrapper(const A1 &a1) : obj_(a1) {}      template <class A1, class A2>     HeapWrapper(const A1 &a1, const A2 &a2) : obj_(a1, a2) {}      // etc  #else     HeapWrapper() : obj_(new T()) {}     ~HeapWrapper() { delete obj_; }      template <class A1>     HeapWrapper(const A1 &a1) : obj_(new T(a1)) {}      template <class A1, class A2>     HeapWrapper(const A1 &a1, const A2 &a2) : obj_(new T(a1, a2)) {}      // etc #endif  #ifdef USE_STACK     operator const T &() const    { return obj_; }     operator T &()                { return obj_; }     T *operator->()               { return &obj_; }     T& operator*()                { return obj_; } #else     operator const T &() const    { return *obj_; }     operator T &()                { return *obj_; }     T *operator->()               { return obj_; }     T& operator*()                { return *obj_; } #endif      // cast operators makes this work nicely     HeapWrapper &operator=(const T &rhs) { *obj_ = rhs; return *this;} };   class A { public:     void member(int x) {         std::cout << x << std::endl;     } };   int main() {     HeapWrapper<int> x1(5);     HeapWrapper<int> x2;     HeapWrapper<int> x3 = x1;     HeapWrapper<int> x4 = 3;      std::cout << x1 << ' ' << x2 << ' ' << x3 << ' ' << x4 << std::endl;      // example using a custom class's members..     HeapWrapper<A> a1;     a1->member(5); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to define the weekly period I would like to use
Is there a simple way to define a breakpoint, in Visual C++ 2005, that
Premise I believe that there is a way to objectively define "Good" and "Bad"
I am trying to convert a macro in C to something that would work
Is there a way to define the timezone for an application in ASP.NET such
Is there a way to define styles for a combination of classes? For example,
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there any syntactical way in jQuery to define multiple CSS attributes without stringing
I wonder if there is a way to set the value of #define in
I'm trying to do some kind of Macro Overloading, so that MACRO(something), gets expanded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.