App*_*ker 2 c++ string templates cstring lpcstr
当我尝试这样做时,我只是在搞乱模板:
template<typename T> void print_error(T msg)
{
#ifdef PLATFORM_WIN32
::MessageBox(0, reinterpret_cast< LPCSTR >(msg), "Error", MB_ICONERROR|MB_OK);
#else
cout << msg << endl;
#endif /* PLATFORM_WIN32 */
}
Run Code Online (Sandbox Code Playgroud)
当然,如果你通过std::string
as,这显然是行不通的T
.因为字符串不能转换为a char*
,但是这个函数是否可以以允许我传递c样式char*
数组和c ++ std::string
作为参数的方式进行编码,并将它们转换为LPCSTR
?
您可以使用函数重载:
void print_error(char const* msg);
void print_error(std::string const& msg);
...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
396 次 |
最近记录: |