Nas*_*ter 0 c++ const function char
我做了一个回复当前日期的函数.在函数内部,我"cout"结果并且它工作但是当我"cout"函数时,它不起作用.我得到了垃圾.
const char* engineCS::getDate() const
{
time_t t = time(0);
struct tm *now = localtime(&t);
char buf[20];
strftime(buf, sizeof(buf), "%Y-%m-%d %X", now);
cout << buf << endl;
return buf;
}
Run Code Online (Sandbox Code Playgroud)
例子:内部:2012-02-02 00:00:00外:????? fv
怎么了?类似的问题:函数和返回const char*
谢谢
编辑:现在有什么问题?对不起,我做了太多的VB.NET ...
const char* engineCS::getDate() const
{
time_t t = time(0);
struct tm *now = localtime(&t);
char *buf;
buf = new char[20];
strftime(buf, sizeof(buf), "%Y-%m-%d %X", now);
cout << buf << endl;
return buf;
}
Run Code Online (Sandbox Code Playgroud)
改变你的功能返回std::string,一切都会好的.除了返回类型之外,您不需要进行任何进一步的更改.如果使用者需要raw char const *,请c_str()在结果字符串上调用成员函数.
| 归档时间: |
|
| 查看次数: |
4508 次 |
| 最近记录: |