如何創建指向對象的動態指針數組的深層副本?我相信這個代碼,我只是爲指向對象的指針分配新的內存,但他們仍然指向相同的位置。因此,當我更改「複製」圖像時,原始圖像也會更改,反之亦然。如何創建指向對象的指針的複製構造函數(深度複製)?
謝謝!
declatration:
Class Scene
{
public:
.
.
.
.
private:
Image ** sceneImage;
int * coordinateX;
int * coordinateY;
int inputMax;
};
在拷貝構造函數
...
Scene::Scene (const Scene & source)
{
inputMax = source.inputMax;
sceneImage = new Image*[inputMax];
coordinateX = new int[inputMax];
coordinateY = new int[inputMax];
// copy even null indexes, because you can put images on null indexes
for (int i = 0; i < inputMax; i++)
{
sceneImage[i] = source.SceneImage[i];
coordinateX[i] = source.coordinateX[i];
coordinateY[i] = source.coordinateY[i];
}
}