Boost::make_shared: A Powerful Tool for Shared Pointer Creation
The 'boost/make_shared.hpp' header provides the 'boost::make_shared' function template, which is an extension to the standard 'std::make_shared' function.
'boost::make_shared' creates a shared pointer to a newly created object of the specified type with the specified constructor arguments, and returns the shared pointer. This function is more efficient than creating the object and then creating the shared pointer separately, because it allocates the memory for the object and the control block in one operation.
Here's an example of using 'boost::make_shared':
#include <boost/make_shared.hpp>
#include <iostream>
#include <string>
int main() {
auto str_ptr = boost::make_shared<std::string>("Hello, world!");
std::cout << *str_ptr << std::endl;
return 0;
}
This creates a shared pointer to a 'std::string' object that contains the string 'Hello, world!', and then prints the string to the console.
原文地址: https://www.cveoy.top/t/topic/nPID 著作权归作者所有。请勿转载和采集!