| | |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







