|
| 1 | +--- |
| 2 | +Title: '.get()' |
| 3 | +Description: 'Returns a raw pointer to the managed object without transferring ownership.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Objects' |
| 9 | + - 'Pointers' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-c-plus-plus' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +The **`.get()`** function in C++ is used with smart pointers such as `std::unique_ptr` and `std::shared_ptr` to obtain the raw pointer to the managed object without transferring ownership. It is commonly used when working with legacy code or APIs that require raw pointers, or when there is a need to access the underlying object directly while still maintaining smart pointer ownership and automatic memory management. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +ptr.get(); |
| 21 | +``` |
| 22 | + |
| 23 | +Here, `ptr` is a `std::unique_ptr<T>` or `std::shared_ptr<T>`. |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `.get()` does not take any parameters. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +- Returns the raw pointer (`T*`) to the managed object. |
| 32 | +- Ownership remains with the smart pointer; the caller must not delete the returned pointer. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +This example shows how the `.get()` method returns a raw pointer to a managed object without transferring ownership: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <memory> |
| 41 | + |
| 42 | +int main() { |
| 43 | + std::unique_ptr<int> uniq_ptr(new int(20)); |
| 44 | + |
| 45 | + int* raw_ptr = nullptr; |
| 46 | + raw_ptr = uniq_ptr.get(); // returns pointer to a managed object without transferring ownership |
| 47 | + |
| 48 | + std::cout << "value at raw_ptr address: " << *raw_ptr << std::endl; |
| 49 | + return 0; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +The output of this example is: |
| 54 | + |
| 55 | +```shell |
| 56 | +value at raw_ptr address: 20 |
| 57 | +``` |
| 58 | + |
| 59 | +In this example: |
| 60 | + |
| 61 | +- The `uniq_ptr` manages a dynamically allocated `int` with a value of 20. |
| 62 | +- The `.get()` method returns a raw pointer to the managed object, so `raw_ptr` points to the same memory location as `uniq_ptr`, but without taking ownership. |
| 63 | +- `raw_ptr` can access (read or write) the value, while `uniq_ptr` still maintains exclusive ownership and will automatically delete the object when it goes out of scope. |
| 64 | + |
| 65 | +## Codebyte Example |
| 66 | + |
| 67 | +This example shows how `.get()` can be used to pass a raw pointer to a legacy function that does not accept smart pointers: |
| 68 | + |
| 69 | +```codebyte/cpp |
| 70 | +#include <iostream> |
| 71 | +#include <memory> |
| 72 | +
|
| 73 | +void legacyPrint(int* ptr) { |
| 74 | + std::cout << "Value from legacy function: " << *ptr << std::endl; |
| 75 | +} |
| 76 | +
|
| 77 | +int main() { |
| 78 | + std::unique_ptr<int> uniq_ptr(new int(42)); |
| 79 | + legacyPrint(uniq_ptr.get()); // Passing raw pointer without giving up ownership |
| 80 | + return 0; |
| 81 | +} |
| 82 | +``` |
0 commit comments