RSS Forums RSS

Is there a way to prevent creating objects on heap?

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 14
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

Question Is there a way to prevent creating objects on heap?

  #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:

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?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,836
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: 1230
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #2  
Dec 12th, 2007
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.
}
Reply With Quote  
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?

  #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 8:07 pm. Reason: clarification
Reply With Quote  
Posts: 1,087
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?

  #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  
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?

  #5  
Dec 12th, 2007
Thank you, vijayan121. I am going to take a look at the item.
Reply With Quote  
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?

  #6  
Dec 12th, 2007
I've gotten the point and solution
Thank you guys!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Views: 1298 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:49 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC