遇到了一个场景,我无法弄清楚实际发生的情况。在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