c++,当const char*为0时,不能将其直接赋给string

下面程序会崩溃:

const char* t_objName = (obj!=NULL)?obj->getName(): 0;

string objName=t_objName;

cout<<objName<<endl;

应改为:

const char* t_objName = (obj!=NULL)?obj->getName(): 0;

string objName=(t_objName==0)?"":t_objName;

cout<<objName<<endl;

原文地址:https://www.cnblogs.com/wantnon/p/4524829.html