January 2020
Intermediate to advanced
454 pages
11h 25m
English
std::shared_ptr is used to manage a pointer when more than one thing must own the pointer for the application to execute properly. Suppose, however, that you provide an API that must accept an integer pointer, as follows:
void execute_threads(int *ptr);
The preceding API suggests that whoever calls this function owns the integer pointer. That is, whoever calls this function is required to allocate the integer pointer, as well as delete it once the function is complete. If, however, we intend for the preceding API to own the pointer, we really should write this API as follows:
void execute_threads(std::unique_ptr<int> ptr);
This API says, please allocate me an integer pointer, but I own it once it's passed and will ensure it ...
Read now
Unlock full access