Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Shared Pointer Functions to Remember | Shared Pointers
course content

Course Content

C++ Smart Pointers

Shared Pointer Functions to RememberShared Pointer Functions to Remember

We have already explored what shared pointers are, and how to create and share them. Now, let’s look at some important functions and operators that make shared pointers even more powerful and flexible.

The reset function

The reset function allows you to reset a shared pointer, i.e. release its ownership of the dynamic object. When you call reset, the reference count decreases, and if there are no other shared pointers referencing the pointed object, the memory is automatically deallocated.

Note

If other shared pointers reference the object, the reference count decreases. Nonetheless, the object persists until all shared pointers are reset or go out of scope. This function serves two purposes effectively.

When reassigning a shared pointer

When you pass a new object as an argument to the reset function, the shared pointer starts pointing to the new object.

In the above code, we are reassigning sharedPtr to a new instance of MyClass.

When releasing ownership

If you call reset without any parameters, the shared pointer simply stops pointing to the dynamic object.

In the above code, we are simply releasing the ownership of the dynamic integer from sharedInt.

The swap function

Use the swap function when you want to swap the contents of two shared pointers. Remember that when you swap two shared pointers, their reference counts and ownership of the dynamic objects are also swapped.

In the above code, we are swapping two shared pointers that both point to dynamic integers. After the code is executed, sharedInt1 will be holding the value 99, whereas sharedInt2 will be holding the value 42.

The get function

Just like unique pointers, every shared pointer holds a raw pointer to the dynamic object. You can use the get function to retrieve the raw pointer, without affecting the reference count.

In the above code, we are extracting a raw pointer from a shared pointer. This can be necessary when working with legacy code that doesn’t support smart pointers.

The use_count function

The use_count function allows you to inspect the current reference count of a shared pointer. It is especially useful for debugging, and for ensuring that shared ownership is working as expected.

In the above code, we use the use_count function to get the current reference count of the shared pointer.

Shared pointer operators

Shared pointers provide several operators to make C++ code more concise and readable. For example:

  • The == and != operators allow you to compare a shared pointer with another shared pointer, or with nullptr.
  • The dereference operator * provides access to the dynamic object pointed to by the shared pointer.
  • The arrow operator -> makes it easy to access the members of the object pointed to by the shared pointer.

What happens when we call swap on a shared pointer?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
course content

Course Content

C++ Smart Pointers

Shared Pointer Functions to RememberShared Pointer Functions to Remember

We have already explored what shared pointers are, and how to create and share them. Now, let’s look at some important functions and operators that make shared pointers even more powerful and flexible.

The reset function

The reset function allows you to reset a shared pointer, i.e. release its ownership of the dynamic object. When you call reset, the reference count decreases, and if there are no other shared pointers referencing the pointed object, the memory is automatically deallocated.

Note

If other shared pointers reference the object, the reference count decreases. Nonetheless, the object persists until all shared pointers are reset or go out of scope. This function serves two purposes effectively.

When reassigning a shared pointer

When you pass a new object as an argument to the reset function, the shared pointer starts pointing to the new object.

In the above code, we are reassigning sharedPtr to a new instance of MyClass.

When releasing ownership

If you call reset without any parameters, the shared pointer simply stops pointing to the dynamic object.

In the above code, we are simply releasing the ownership of the dynamic integer from sharedInt.

The swap function

Use the swap function when you want to swap the contents of two shared pointers. Remember that when you swap two shared pointers, their reference counts and ownership of the dynamic objects are also swapped.

In the above code, we are swapping two shared pointers that both point to dynamic integers. After the code is executed, sharedInt1 will be holding the value 99, whereas sharedInt2 will be holding the value 42.

The get function

Just like unique pointers, every shared pointer holds a raw pointer to the dynamic object. You can use the get function to retrieve the raw pointer, without affecting the reference count.

In the above code, we are extracting a raw pointer from a shared pointer. This can be necessary when working with legacy code that doesn’t support smart pointers.

The use_count function

The use_count function allows you to inspect the current reference count of a shared pointer. It is especially useful for debugging, and for ensuring that shared ownership is working as expected.

In the above code, we use the use_count function to get the current reference count of the shared pointer.

Shared pointer operators

Shared pointers provide several operators to make C++ code more concise and readable. For example:

  • The == and != operators allow you to compare a shared pointer with another shared pointer, or with nullptr.
  • The dereference operator * provides access to the dynamic object pointed to by the shared pointer.
  • The arrow operator -> makes it easy to access the members of the object pointed to by the shared pointer.

What happens when we call swap on a shared pointer?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
some-alt