/*
string和c-style字符串转换
*/
void func(string s)
{
cout << s << endl;
}
void func2(const char * s)
{
cout << s << endl;
}
void test07()
{
string s = "abc";
//string -> const char *
const char * p = s.c_str();
func(p); //const char * 隐式类型转换为 string
//const char * -> string
string s2(p);
//func2(s2); //string 不能隐式类型转换为 char *
}
总结:
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string str(s);