遇到了一個場景,我無法弄清楚實際發生的情況。在cpp中處理字符串參考
//這是CPP計劃。
#include <iostream>
#include <time.h>
using namespace std;
int main() {
std::string nums ("1234567890"); //What happens with this memory??
std::string alpha ("abcdefghijklmnopqrstuvwxyz");
std::string &charStr = nums;
charStr = alpha;
cout <<"nums : " << nums <<endl;
cout <<"alpha :" << alpha <<endl;
cout <<"charStr :" << charStr <<endl;
return 0;
}
輸出是:
NUMS:ABCDEFGHIJKLMNOPQRSTUVWXYZ
阿爾法:ABCDEFGHIJKLMNOPQRSTUVWXYZ
charStr:ABCDEFGHIJKLMNOPQRSTUVWXYZ
與時間分配給NUMS內存會發生什麼初始補腎中藥?
它被分配在堆棧上。 – leppie 2015-01-21 05:40:03