C++ memory make_shared() Function
Example
Create a shared_ptr to a string:
shared_ptr<string> car = make_shared<string>("Volvo");
cout << *car;
Definition and Usage
The make_shared() function gets new memory, and stores a value in it. Then it returns a shared_ptr that points to the memory.
Write the data type between < and >, and the value between the parentheses.
The make_shared function is defined in the <memory> header file.
Syntax
make_shared<type>(value);
type is the data type of the value, like int or string.
Parameter Values
| Parameter | Description |
|---|---|
| value | Optional. The start value of the new memory. |
Technical Details
| Returns: | A shared_ptr that points to the new memory. |
|---|
Related Pages
Read more about smart pointers in our Smart Pointers Tutorial.