【发布时间】:2011-04-01 16:00:55
【问题描述】:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
const char* funA()
{
return "aa"; // where does system to store this temporary variable?
}
// this is not an valid function
const char* funB()
{
string str("bb");
return str.c_str();
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << funA() << endl;
cout << funB() << endl; // invalid
return 0;
}
问题> 我们不应该在函数内返回指针或对局部变量的引用。 所以返回变量“aa”不是 funA 函数内部的局部变量。那是什么?
谢谢
【问题讨论】:
-
没有它存储的地方只是返回发送到输出屏幕
标签: c++ static object-lifetime