Why would you use something like that?
The child class constructor is the method/procedure that is called when the object is created. Putting a parameter in there that references the parent class forces anybody using the TChildClass to specify a parent.... which seems to be exactly what your design wants.
However, you can remove that parameter from the constructor, and do something like this (not tested):
type
TParentClass = class;
TChildClass = class(TThread)
FParent: TParentClass;
procedure Execute; override; //<<< the Inside procedure
constructor Create(CreateSuspended: Boolean; AParent:
TParentClass; const StackSize: SizeUInt = DefaultStackSize);
property Parent: TParentClass read FParent write FParent;
end;
///////////////////////////////////////////////////
type
TParentClass= class(Tobject)
ChildClass : TChildClass;
EndProc : procedure of object; //< Procedure is declared outside the thread.
constructor Create();
end;
////////////////////////////////////////////////////
Procedure TChildClass.Execute; //<<< the Inside procedure
begin
Parent.EndProc ; //<<< the Outside procedure
end;
/////////////////////////////////////////////////////
constructor TChildClass.Create(CreateSuspended: Boolean;
const StackSize: SizeUInt);
begin
inherited Create(CreateSuspended,StackSize);
FreeOnTerminate := True;
// we can't do this anymore:
//Parent:=AParent;
// If we did, directly accessing the variable would be
// 1. possible - we're inside the class that has it, so the private section variables are available
// 2. a bit faster - no need to go through the property, directly access the variable FParent
// 3. a bit cleaner depending on what you want: you bypass the property, which can have a Set... procedure which can change other variables in your class which may be unexpected/unwanted
// .... so:
// FParent:=AParent;
end;
/////////////////////////////////////////////////////////
constructor TParentClass.Create;
begin
inherited Create;
//Now we cannot set the child's parent to ourself. This is not an improvement, actually:
//ChildClass :=TChildClass.Create(true,self); //<<<<< Here with self, assign parent
ChildClass :=TChildClass.Create(true);
//... and we have to do it manually...
ChildClass.Parent:=self;
end;
In your example, I'd think that every child must have a parent. In other examples, that may not be so - e.g. I'm working with test cases that can have a hierarchy, so they have parents. However, at the top of the hierarchy, there is no parent.
In that case, not forcing a parent through a constructor is a good idea....