| | |
Is there a way to prevent creating objects on heap?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Yesterday I saw a question that was asking to prevent creating objects on heap.
After some thoughts, I came out the following:
But this can only guarantee the first object is on stack and it can’t go further because there is a public copy ctor.
I haven't seen the point of the question, but it's fun to think about; so my question: Is there a way to do so?
After some thoughts, I came out the following:
C++ Syntax (Toggle Plain Text)
class Foo { public: Foo(const Foo& f) {} static Foo getAnInstance() { return Foo(); } private: Foo() {} };
But this can only guarantee the first object is on stack and it can’t go further because there is a public copy ctor.
I haven't seen the point of the question, but it's fun to think about; so my question: Is there a way to do so?
methods of a class are never ever on the stack -- only data objects go there.
C++ Syntax (Toggle Plain Text)
int main() { Foo f; // object f goes on the stack, but not the methods of that object. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
see: More Effective C++: 35 More Ways to Improve Your Programs and Designs (Scott Meyers)
ITEM 27. Requiring or Prohibiting Heap-Based Objects.
and http://www.aristeia.com/BookErrata/M...ts_frames.html
ITEM 27. Requiring or Prohibiting Heap-Based Objects.
and http://www.aristeia.com/BookErrata/M...ts_frames.html
![]() |
Other Threads in the C++ Forum
- Previous Thread: plz help i have prblm in Borlandc++ graphic.h
- Next Thread: String Sorting Help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets







