char *p = "";
std::string strP = p; // 深拷贝
delete[] p; // 需要清空内存
需要注意下面这种情况:
char *p = NULL;
std::string strP = p; // string不接受NULL赋值
delete[] p;
会造成程序崩溃,所以char*赋值给string时,一定要判空:
char *p = NULL;
if (p == NULL)
{
return;
}
std::string strP = p;