我一定在这里错过了一个显而易见的事实 - 有一段时间没有编程C++。为什么不能在将c风格字符串分配给const char *变量后打印它?但是,如果我尝试直接打印,无需进行指定正常工作:C++ const char *指针赋值
#include "boost/lexical_cast.hpp"
using namespace std;
using boost::lexical_cast;
int main (int argc, char** argv)
{
int aa=500;
cout << lexical_cast<string>(aa).c_str() << endl; // prints the string "500" fine
const char* bb = lexical_cast<string>(aa).c_str();
cout << bb << endl; // prints nothing
return EXIT_SUCCESS;
}
[打印500!](http://ideone.com/A0tuqQ) –
@MarkGarcia:这是未定义的行为。所以做你期望它是一个完美的实现选项。 –
@BenjaminLindley当然。只显示一个真实的输出。 :) –