title: Can a C++ class determine whether it's on the stack or heap?
body: <p>I have </p>
  
  <pre><code>class Foo {
  ....
  }
  </code></pre>
  
  <p>Is there a way for Foo to be able to separate out:</p>
  
  <pre><code>function blah() {
    Foo foo; // on the stack
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>function blah() {
    Foo foo* = new Foo(); // on the heap
  }
  </code></pre>
  
  <p>I want Foo to be able to do different things depending on whether it's allocated on the Stack or the Heap.</p>
  
  <p>Edit:</p>
  
  <p>Alof of people have asked me "why do this?"</p>
  
  <p>The answer:</p>
  
  <p>I'm using a ref-counted GC right now. However, I want to have ability to run mark &amp; sweep too. For this, I need to tag a set of "root" pointers -- these are the pointers on the stack. Thus, for each class, I'd like to know whether they're in the stack or in the heap.</p>