Is there a way to prevent creating objects on heap?

Thread Solved
Reply

Join Date: Oct 2007
Posts: 14
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

Is there a way to prevent creating objects on heap?

 
0
  #1
Dec 12th, 2007
Yesterday I saw a question that was asking to prevent creating objects on heap.

After some thoughts, I came out the following:

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,167
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1438
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

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

 
0
  #2
Dec 12th, 2007
methods of a class are never ever on the stack -- only data objects go there.
  1. int main()
  2. {
  3. Foo f; // object f goes on the stack, but not the methods of that object.
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

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

 
0
  #3
Dec 12th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

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

 
0
  #4
Dec 12th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

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

 
0
  #5
Dec 12th, 2007
Thank you, vijayan121. I am going to take a look at the item.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

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

 
0
  #6
Dec 12th, 2007
I've gotten the point and solution
Thank you guys!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC