Memory Management

Started by
15 comments, last by Matt-D 10 years, 10 months ago

I'm guessing CObject is a base class of some sort (I'm referring to the push_back solution, which would otherwise be my choice as well).

I don't think so. His code with `new CObject(0, 0, 1);' doesn't allow for a derived class to be instantiated.

Advertisement

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:


ObjectList.push_back(new CObject(0, 0, 1)); 
 

Thanks for all the feedback.

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:


ObjectList.push_back(new CObject(0, 0, 1)); 
 

Thanks for all the feedback.

Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

In my program, a single object sometimes needs to have its address in two different lists at the same.


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

In my program, a single object sometimes needs to have its address in two different lists at the same.

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

I'll look into those. Thanks.

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

I'll look into those. Thanks.

Just go through this: http://www.informit.com/articles/article.aspx?p=1944072

// a chapter from this, a great book, BTW: http://www.informit.com/store/c-plus-plus-primer-9780321714114

And then this:

http://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/
http://herbsutter.com/2013/05/30/gotw-90-solution-factories/
http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/

Should be all you need to know to get started and do it right! :-)

This topic is closed to new replies.

Advertisement