Reputation:
This is a design question for Delphi.
An Object A contains a set of variables and at the same time stores a list of Objects of type B. Objects of type B need to access the common variables in the container Object A. One option is for Objects B to hold a reference to the container Object A but this seems to result in storing the reference to A as a TObject and casing each time to Object A in order to access the variables. Any other possible design solutions?
Object B
// Possible solution
ref to Object A
Object A
x : integer
list of B
Each B needs access to x
Upvotes: 1
Views: 466
Reputation: 613352
You need a forward type declaration:
type
TContainer = class; // forward declaration
TItem = class
private
FContainer: TContainer;
....
end;
TContainer = class
private
// list of items
end;
The forward declaration can be either the container or the item, it doesn't much matter which.
Read all about this in the docs: http://docwiki.embarcadero.com/RADStudio/en/Classes_and_Objects#Forward_Declarations_and_Mutually_Dependent_Classes
Upvotes: 2