If you add classes to your component, the component's constructor must initialize them so that the user can interact with the objects at runtime. Similarly, the component's destructor must also destroy the owned objects before destroying the component itself.
constructor TSampleShape.Create(AOwner: TComponent); begin inherited Create(AOwner); { always call the inherited constructor } Width := 65; Height := 65; FPen := TPen.Create; { construct the pen } FBrush := TBrush.Create; { construct the brush } end;
__fastcall TSampleShape::TSampleShape(TComponent* Owner) : TGraphicControl(Owner) { Width = 65; Height = 65; FBrush = new TBrush(); // construct the pen FPen = new TPen(); // construct the brush }
type TSampleShape = class(TGraphicControl) public { destructors are always public} constructor Create(AOwner: TComponent); override; destructor Destroy; override; { remember override directive } end;
class PACKAGE TSampleShape : public TGraphicControl { . . . public: // destructors are always public virtual __fastcall TSampleShape(TComponent* Owner); __fastcall ~TSampleShape(); // the destructor . . . };
destructor TSampleShape.Destroy; begin FPen.Free; { destroy the pen object } FBrush.Free; { destroy the brush object } inherited Destroy; { always call the inherited destructor, too } end;
__fastcall TSampleShape::~TSampleShape() { delete FPen; // delete the pen object delete FBrush; // delete the brush object }
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|