C++ C++;——系统在哪里存储返回的字符? #包括“stdafx.h” #包括 #包括 使用名称空间std; 常量char*funA() { 返回“aa”;//系统在哪里存储这个临时变量? } //这不是一个有效的函数 常量char*funB() { 字符串str(“bb”); 返回str.c_str(); } int _tmain(int argc,_TCHAR*argv[] { cout

C++ C++;——系统在哪里存储返回的字符? #包括“stdafx.h” #包括 #包括 使用名称空间std; 常量char*funA() { 返回“aa”;//系统在哪里存储这个临时变量? } //这不是一个有效的函数 常量char*funB() { 字符串str(“bb”); 返回str.c_str(); } int _tmain(int argc,_TCHAR*argv[] { cout,c++,static,object-lifetime,C++,Static,Object Lifetime,“aa”是一个字符串文本,因此它具有静态存储持续时间。这意味着它从程序开始到结束都存在。它没有在堆栈或空闲存储(堆)上显式分配 唯一的临时对象是指向该字符串文本的指针,该指针由值返回(这意味着返回该字符串文本的副本)。no它存储的位置只是返回发送到输出屏幕 #include "stdafx.h" #include <iostream> #include <string> using namespace std; const char* funA() { retu

“aa”
是一个字符串文本,因此它具有静态存储持续时间。这意味着它从程序开始到结束都存在。它没有在堆栈或空闲存储(堆)上显式分配


唯一的临时对象是指向该字符串文本的指针,该指针由值返回(这意味着返回该字符串文本的副本)。

no它存储的位置只是返回发送到输出屏幕
#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;
}