You cannot convert a string literal to a shared pointer. Let me just “correct” your code, and then all you have remaining is undefined behavior:
const char *str = "abcdfg";
boost::shared_ptr<char> ss(str);
Now, this will compile, but it will produce serious problems because str
is not memory that was allocated dynamically. As soon as the shared pointer is destructed, you will get undefined behavior.
So, if you want that string, you will have to copy it first:
const char *str = "abcdfg";
boost::shared_ptr<char> ss( new char[std::strlen(str)+1] );
std::strcpy( ss.get(), str );
But if you’re just doing this to have RAII semantics on a string, why not use std::string
in the first place?
std::string sss( str );
solved how to convert char * to boost::shared_ptr?