C++ memory get() Function
Example
Get the normal pointer from a unique_ptr:
unique_ptr<int> ptr(new int(25));
int* raw = ptr.get();
cout << *raw;
Definition and Usage
The get() function returns the normal pointer (also called a raw pointer) that a smart pointer holds. The smart pointer is still the owner of the memory.
This is useful when you need to give a normal pointer to a function. Do not use delete on the pointer, because the smart pointer frees the memory.
The get function is defined in the <memory> header file.
Syntax
ptr.get();
ptr is a unique_ptr or a shared_ptr.
Parameter Values
None.
Technical Details
| Returns: | A normal pointer to the memory, or nullptr if the smart pointer is empty. |
|---|
Related Pages
Read more about smart pointers in our Smart Pointers Tutorial.