943,908 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2463
  • C++ RSS
Dec 12th, 2007
0

Is there a way to prevent creating objects on heap?

Expand Post »
Yesterday I saw a question that was asking to prevent creating objects on heap.

After some thoughts, I came out the following:

C++ Syntax (Toggle Plain Text)
  1. class Foo
  2. {
  3. public:
  4. Foo(const Foo& f) {}
  5. static Foo getAnInstance()
  6. {
  7. return Foo();
  8. }
  9. private:
  10. Foo() {}
  11. };

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?
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jaux is offline Offline
15 posts
since Oct 2007
Dec 12th, 2007
0

Re: Is there a way to prevent creating objects on heap?

methods of a class are never ever on the stack -- only data objects go there.
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. Foo f; // object f goes on the stack, but not the methods of that object.
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 12th, 2007
0

Re: Is there a way to prevent creating objects on heap?

Thank you, Dragon. Does that mean that, on our side (not the client side), to restrict the location of object creation is pointless? I mean that it's client's responsibility to determine where his/her objects should be located.
Last edited by jaux; Dec 12th, 2007 at 9:07 pm. Reason: clarification
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jaux is offline Offline
15 posts
since Oct 2007
Dec 12th, 2007
0

Re: Is there a way to prevent creating objects on heap?

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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Dec 12th, 2007
0

Re: Is there a way to prevent creating objects on heap?

Thank you, vijayan121. I am going to take a look at the item.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jaux is offline Offline
15 posts
since Oct 2007
Dec 12th, 2007
0

Re: Is there a way to prevent creating objects on heap?

I've gotten the point and solution
Thank you guys!
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jaux is offline Offline
15 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: plz help i have prblm in Borlandc++ graphic.h
Next Thread in C++ Forum Timeline: String Sorting Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC