Say we have a base class A which is abstract:
abstract class A {
//code for class A
}
Also we have a subclass B which inherits from A and contains a copy constructor:
class B : A {
//some other membervariables and methods
public B (B copy) {
//makes a copy
}
}
Now suppose we make a class GridObject which we want to inherit from A, and the grid elements it contains are also a type of A, but might be of type B or any other class that inherits from A:
class GridObject : A {
A[,] grid;
public GridObject (int columns, int rows) {
grid = new A[columns, rows];
}
public GridObject (GridObject copy) {
//makes a copy
}
public Add (A obj, int x, int y) {
grid[x,y] = obj;
}
Now, my problem is how to make a copy constructor that makes an exact copy of the GridObject. Suppose we do this:
B b = new B();
GridObject gridObject = new GridObject(5, 5);
for (int x = 0; x < 5; x++)
for(int y = 0; y < 5; y++)
gridObject.Add(new B (b), x, y);
Now, what is the best way to implement a (deep) copy constructor for the class GridObject? Note that the elements of our gridObject are of type B now. But this also might have been any other arbitrary class that inherits from A.