Is a smart pointer that holds a non owning reference to an object that is managed by a pointer which has ownership on it?

Is a smart pointer that holds a non owning reference to an object that is managed by a pointer which has ownership on it?

weak_ptr. It is defined as a smart pointer that holds a non-owning (weak) reference to an object that is managed by another std::shared_ptr . The syntax to declare a weak pointer is : std::weak_ptr p_weak1(p_shared);

What happens when a unique pointer goes out of scope?

A std::unique_ptr owns of the object it points to and no other smart pointers can point to it. When the std::unique_ptr goes out of scope, the object is deleted. This is useful when you are working with a temporary, dynamically-allocated resource that can get destroyed once out of scope.

How to create vector of shared pointers to objects?

I believe that I can either create a vector of vector of shared pointers to Contact objects, vector >> prtContacts (where I have left off std:: for ease of viewing), so that prtContacts [i] will contain a vector of shared pointers to the contacts of the i th particle.

What happens when you delete a vector of pointers?

Deleting the vector will result in memory leaks, as the objects pointed to by its elements are not deleted. TL;DR: The objects remain, the pointers are lost == memory leak. Your second case works just fine, since the objects a and b are not allocated dynamically. They will be destroyed when they go out of scope.

How to store a vector of pointers in C + +?

The best way to store pointers in a vector will be to use smart_ptr instead of raw pointers. As soon as the vector DTOR is called and control exits the DTOR all smart_ptrs will be refernced counted. And you should never bothered about the memory leak with smart_ptrs.

How are shared pointers used in a particle class?

Particle objects will each have a number of contacts, represented by Contact objects, and contacts are shared by pairs of particles. It will be necessary for me to know what contacts a specific particle has, and I have decided to be able to obtain that information through the Network object (e.g., using network.getContacts (Particle particle) ).