|
| 1 | +--- |
| 2 | +Title: '.reset()' |
| 3 | +Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Containers' |
| 9 | + - 'Pointers' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-c++' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +The **`.reset()`** method is used with smart pointers in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object (deleting it if this is the last owner) and optionally takes ownership of a new object passed as a raw pointer. |
| 16 | + |
| 17 | +This method safely manages dynamic memory by deleting the previously managed object (if any), thereby helping to prevent memory leaks. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ptr.reset(); // Releases ownership and deletes the managed object |
| 23 | +ptr.reset(raw_ptr); // Releases current object and takes ownership of raw_ptr |
| 24 | +``` |
| 25 | + |
| 26 | +## Example |
| 27 | + |
| 28 | +This example demonstrates how `.reset()` releases ownership of the managed object, deletes it, and sets the pointer to null: |
| 29 | + |
| 30 | +```cpp |
| 31 | +#include <iostream> |
| 32 | +#include <memory> |
| 33 | + |
| 34 | +int main() { |
| 35 | + std::unique_ptr<int> ptr(new int(42)); |
| 36 | + std::cout << "Value before reset: " << *ptr << std::endl; |
| 37 | + |
| 38 | + ptr.reset(); // Releases ownership and deletes the managed object |
| 39 | + |
| 40 | + if (!ptr) { |
| 41 | + std::cout << "Pointer is null after reset." << std::endl; |
| 42 | + } |
| 43 | + return 0; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +The output of this code is: |
| 48 | + |
| 49 | +```shell |
| 50 | +Value before reset: 42 |
| 51 | +Pointer is null after reset. |
| 52 | +``` |
| 53 | + |
| 54 | +In this example: |
| 55 | + |
| 56 | +- A `std::unique_ptr` manages an `int` with a value of 42. |
| 57 | +- After calling `.reset()`, the managed object is deleted and the pointer becomes null. |
| 58 | +- The check `if (!ptr)` confirms the pointer was successfully reset. |
| 59 | + |
| 60 | +## Codebyte Example |
| 61 | + |
| 62 | +Run the following example to understand how the `.reset()` method works: |
| 63 | + |
| 64 | +```codebyte/cpp |
| 65 | +#include <iostream> |
| 66 | +#include <memory> |
| 67 | +#include <string> |
| 68 | +
|
| 69 | +class TV { |
| 70 | +public: |
| 71 | + TV(std::string brand) : brand_(brand) { |
| 72 | + std::cout << brand_ << " TV is turned ON.\n"; |
| 73 | + } |
| 74 | + ~TV() { |
| 75 | + std::cout << brand_ << " TV is turned OFF.\n"; |
| 76 | + } |
| 77 | + void watch() const { |
| 78 | + std::cout << "Watching " << brand_ << " TV.\n"; |
| 79 | + } |
| 80 | +
|
| 81 | +private: |
| 82 | + std::string brand_; |
| 83 | +}; |
| 84 | +
|
| 85 | +int main() { |
| 86 | + std::unique_ptr<TV> remote(new TV("Samsung")); // Remote controls Samsung TV |
| 87 | + remote->watch(); |
| 88 | +
|
| 89 | + // Replace old TV with a new LG TV |
| 90 | + remote.reset(new TV("LG")); // Old TV turned off, now controlling LG TV |
| 91 | + remote->watch(); |
| 92 | +
|
| 93 | + // Put down the remote, no TV controlled now |
| 94 | + remote.reset(); // LG TV turned off, remote controls nothing |
| 95 | +
|
| 96 | + if (!remote) { |
| 97 | + std::cout << "Remote controls no TV now.\n"; |
| 98 | + } |
| 99 | +
|
| 100 | + return 0; |
| 101 | +} |
| 102 | +``` |
0 commit comments