<p>Does anyone know how I can, in platform-independent C++ code prevent an object from being created on the heap? That is, for a class "Foo", I want to prevent users from doing this:</p>

<pre><code>Foo *ptr = new Foo;
</code></pre>

<p>and only allow them to do this:</p>

<pre><code>Foo myfooObject;
</code></pre>

<p>Does anyone have any ideas?</p>

<p>Cheers,</p>

[{'answer_id': 11000, 'author': 'David', 'author_id': 381, 'author_profile': 'https://Stackoverflow.com/users/381', 'pm_score': 0, 'selected': False, 'text': '<p>You could declare a function called "operator new" inside the Foo class which would block the access to the normal form of new.</p>\n\n<p>Is this the kind of behaviour you want ?</p>\n'}, {'answer_id': 11003, 'author': 'Nick', 'author_id': 1236, 'author_profile': 'https://Stackoverflow.com/users/1236', 'pm_score': 4, 'selected': False, 'text': "<p>You could overload new for Foo and make it private. This would mean that the compiler would moan... unless you're creating an instance of Foo on the heap from within Foo. To catch this case, you could simply not write Foo's new method and then the linker would moan about undefined symbols.</p>\n\n<pre><code>class Foo {\nprivate:\n  void* operator new(size_t size);\n};\n</code></pre>\n\n<p>PS. Yes, I know this can be circumvented easily. I'm really not recommending it - I think it's a bad idea - I was just answering the question! ;-)</p>\n"}, {'answer_id': 11011, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': -1, 'selected': False, 'text': "<p>Not sure if this offers any compile-time opportunities, but have you looked at overloading the 'new' operator for your class?</p>\n"}, {'answer_id': 11014, 'author': 'pauldoo', 'author_id': 755, 'author_profile': 'https://Stackoverflow.com/users/755', 'pm_score': 3, 'selected': False, 'text': '<p>I don\'t know how to do it reliably and in a portable way.. but..</p>\n\n<p>If the object is on the stack then you might be able to assert within the constructor that the value of \'this\' is always close to stack pointer.  There\'s a good chance that the object will be on the stack if this is the case.</p>\n\n<p>I believe that not all platforms implement their stacks in the same direction, so you might want to do a one-off test when the app starts to verify which way the stack grows..  Or do some fudge:</p>\n\n<pre><code>FooClass::FooClass() {\n    char dummy;\n    ptrdiff_t displacement = &amp;dummy - reinterpret_cast&lt;char*&gt;(this);\n    if (displacement &gt; 10000 || displacement &lt; -10000) {\n        throw "Not on the stack - maybe..";\n    }\n}\n</code></pre>\n'}, {'answer_id': 11018, 'author': 'pauldoo', 'author_id': 755, 'author_profile': 'https://Stackoverflow.com/users/755', 'pm_score': 2, 'selected': False, 'text': "<p>@Nick</p>\n\n<p>This could be circumvented by creating a class that derives from or aggregates Foo.  I think what I suggest (while not robust) would still work for derived and aggregating classes.</p>\n\n<p>E.g:</p>\n\n<pre><code>struct MyStruct {\n    Foo m_foo;\n};\n\nMyStruct* p = new MyStruct();\n</code></pre>\n\n<p>Here I have created an instance of 'Foo' on the heap, bypassing Foo's hidden new operator.</p>\n"}, {'answer_id': 11022, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': '<p>You could declare it as an interface and control the implementation class more directly from your own code.</p>\n'}, {'answer_id': 11299, 'author': 'Patrick Johnmeyer', 'author_id': 363, 'author_profile': 'https://Stackoverflow.com/users/363', 'pm_score': 6, 'selected': True, 'text': '<p><a href="https://stackoverflow.com/questions/10985/how-to-prevent-an-object-being-created-on-the-heap#11003">Nick\'s answer</a> is a good starting point, but incomplete, as you actually need to overload:</p>\n\n<pre><code>private:\n    void* operator new(size_t);          // standard new\n    void* operator new(size_t, void*);   // placement new\n    void* operator new[](size_t);        // array new\n    void* operator new[](size_t, void*); // placement array new\n</code></pre>\n\n<p>(Good coding practice would suggest you should also overload the delete and delete[] operators -- I would, but since they\'re not going to get called it isn\'t <em>really</em> necessary.)  </p>\n\n<p><a href="https://stackoverflow.com/questions/10985/how-to-prevent-an-object-being-created-on-the-heap#11018">Pauldoo</a> is also correct that this doesn\'t survive aggregating on Foo, although it does survive inheriting from Foo.  You could do some template meta-programming magic to HELP prevent this, but it would not be immune to "evil users" and thus is probably not worth the complication.  Documentation of how it should be used, and code review to ensure it is used properly, are the only ~100% way.</p>\n'}, {'answer_id': 37472123, 'author': 'MerkX', 'author_id': 2251119, 'author_profile': 'https://Stackoverflow.com/users/2251119', 'pm_score': 2, 'selected': False, 'text': '<p>Because debug headers can override the operator new signature, it is best to use the ... signatures as a complete remedy:</p>\n\n<pre><code>private:\nvoid* operator new(size_t, ...) = delete;\nvoid* operator new[](size_t, ...) = delete;\n</code></pre>\n'}, {'answer_id': 48359317, 'author': 'Bala', 'author_id': 9245009, 'author_profile': 'https://Stackoverflow.com/users/9245009', 'pm_score': 0, 'selected': False, 'text': '<p>this can be prevented by making constructors private and providing a static member to create an object in the stack </p>\n\n<pre><code>Class Foo\n{\n    private:\n        Foo();\n        Foo(Foo&amp; );\n    public:\n        static Foo GenerateInstance() { \n            Foo a ; return a; \n        }\n}\n</code></pre>\n\n<p>this will make creation of the object always in the stack.</p>\n'}]
['https://Stackoverflow.com/questions/10985', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1304/']