C++ -- How to prevent an object from being allocating on stack?
<p>you have a class A, where you set ctor to be private, so a client can't call</p>

<pre><code>A a;
</code></pre>

<p>to create obj on stack.
But someday another developer adds a new ctor:</p>

<pre><code>A(int)
</code></pre>

<p>and try to call "A a(1);" inside main(). So this will create a obj on stack. How do you prevent that?</p>

<p>Here is my solution but I don't know whether or not it makes sense.</p>

<p>Use private or protected destructor for the class A and provides a factory function to allocate the object and a function to release the resource (i.e. call destructor)</p>

<p>Thank you</p>
