|
|
因为在c++中,数组名称指向数组的第一个元素的地址。
for example:
typedef double[2] double_array;
//here is global declaration of a map object
map<string, double_array*> myMap;
void some_function()
{
//myArray is local variable...
double_array myArray[100];
// some initialization or manipulation of array element
......
// now, save the array into a map
myMap.insert(make_pair("My Array", myArray);
}// some_function ends here
这段程序的问题是,myArray是局部变量,在c++里,数组名称指向数组的第一个元素的地址, 所以myMap.insert(make_pair("My Array", myArray)只是把myArray的地址保存了下来。一旦some_function结束之后,myArray就不再存在了,如果我要用到myMap里的myArray中的任何值,它们也已经不存在了。我的问题是,如何解决这个问题?让myArray还做为局部变量,如何当函数some_function结束时,我还可能从myMap里哪到我原来的myArray???
thanks |
|