Jump to content
  • Advertisement
Sign in to follow this  
jdub

Question about C++ classes/stack and heap

This topic is 3640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have this class: class RandomClass { struct RandomStruct rs; ... }; If I do not instantiate rs with the new operator it lies on the stack right? So What if I decide that I want to instantiate RandomClass with the new operator. Even though I didnt use new on rs will it be on the Heap with my class? Also, I am kinda confused as to why I need new at all. Cant I just do something like this: //global struct RandomStruct rs; int WINAPI WinMain(...) { rs(...) } and use rs throughout my entire program. And One more question: What is the rule of thumb as to when to use the new operator? I see that most people don't "new" bools and ints in Classes, yet some other class members are "newed".

Share this post


Link to post
Share on other sites
Advertisement
Members use the memory location of their container class. See the first part of this article, for instance.

You don't need "new" : you can use the standard library containers to allocate and manage your objects for you. People use "new" when they want to manage the lifetime of their object themselves.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!