Is there a way to prevent creating objects on heap?
Please support our C++ advertiser: Programming Forums
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:
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.
int main()
{
Foo f; // object f goes on the stack, but not the methods of that object.
}•
•
Posts: 1,087
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
•
•
•
•
Views: 1298 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)







Linear Mode