VC CArray 返回值问题

来源:百度知道 编辑:UC知道 时间:2024/06/08 18:59:34
void CPppDlg::OnButton()
{
int Count=0;
int *strPos = StrSearch("ATLAS STEP 1 IS PASSED!", "PASSED", &Count);
//疑问:我的StrSearch函数应该返回一个整形数组的首地址,也就是
thePos.GetData();调试运行时,在StrSearch未返回前,thePos.GetData();数组里面是有一个元素的,但是一返回就销毁了,可是我返回的是指针,怎么会这样呢?难道是CArray的返回另有其法?请大侠们指教。
}

int* CPppDlg::StrSearch(CString source, CString division, int* Count)
{
int pos = 0;
int pre_pos = 0;
int index=0;
CArray<int, int&> thePos;
while( -1 != pos )
{
pos = source.Find(division,(pos+1));
thePos.Add(pos);
index++;
}
*Count = index-1;
return thePos.GetData();
}

在运行
return thePos.GetData();
的时候thePos没有被销毁,所以可以返回地址。

但当StrSearch函数执行完成后,thePos就被销毁了,thePos里的内容就都没有了,所以指针向的空间还是存在的,但里面的内容就没有了。

可以将把thePos定义为全局变量,或者CPppDlg的成员变量。