How do I define a class that can only be instantiated via new (ie not on the stack)?
<p>I want to arrange things such that for a class Foo, this works:</p>

<pre><code>Foo *f = new Foo()
</code></pre>

<p>but this does not (preferably a compile-time error):</p>

<pre><code>Foo f;
</code></pre>

<p>Can this be done?</p>

<p>The specific reason is that Foo is actually a tracking class to make another heap-allocated object appear in a script environment. Foo's internals keeps a mapping from script references to C++ objects. The tracking object would be used like this:</p>

<pre><code>Thing *t = new Thing();
(new FooThing(t))-&gt;PushToScriptStack();
</code></pre>

<p>If the tracking object is on the stack then the pointer becomes invalid at the end of scope, and I'm left with dangling pointers in my tracking data.</p>

<p>I have all this working well, I'm just looking for a little extra protection.</p>
