ThN
ThN

Reputation: 3278

Delphi Prism : Is this deepcopy or shallowcopy?

Take a look at the following code:

method TMakerRect.Clone: TMakerObject;
var
  newRect:TMakerRect;
begin
  newRect := new TMakerRect(bounds,myForm);
  newRect.Assign(Self);
  newRect.theBrushStyle := Self.theBrushStyle;
  Result := newRect;
end;

method TMakerGraphic.Assign(obj:TMakerObject);
begin
  inherited Assign(obj);
  if obj is TMakerGraphic then
  begin
       thePen:=TmakerGraphic(obj).thePen;
       FillColor:=TMakerGraphic(obj).fillColor;
       dynamics:=TmakerGraphic(obj).dynamics;
  end;
end;

I am thinking that this is how you would do deepcopy cloning of an object. If that is true, these objects should act as if they are separate object, but it doesn't. Any time I change, for instance, thepen's width it also changes the thepen width of the original object. Please, help.

Thanks in advance.

Upvotes: 0

Views: 92

Answers (1)

Roman Starkov
Roman Starkov

Reputation: 61382

This is a shallow clone; thePen is not cloned but shared between the two instances.

Instead of

newRect.Assign(Self);

you should clone each individual property, something like this (note: pseudocode)

newRect.thePen := self.thePen.Clone();
etc...

Upvotes: 1

Related Questions