我创建了一个指向双数组的指针向量。这些指针在函数(即InsertIntoVector
)退出后是否有效?如果我稍后获取指针GetVecElement
,是否仍然保证指针指向它们分配的相同内存位置?
class A {
vector<double*> vec;
void insertIntoVector(double x, double y);
void GetVecElement(int i, double& x, double& y);
};
A::insertIntoVector(double x, double y) {
double* xy = new double[2];
xy[0] = x; xy[1] = y;
vec.push_back(xy);
}
A::GetVecElement(int i, double& x, double& y)
{
x = vec[i][0]; // will the reference to the double array still be valid?
y = vec[i][1];
}