Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of Descendent Classes

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
I haven't found any good examples which indicate this can be done (or is smart to do), so I'm going to ask. Let's say I have:

TParent
TChild1
TChild2
etc

Where each child is distinct in some way. Now, what I have in mind is to bind them together into a TParentList (descendent from TList). Then when the TParentList is iterated, call a method which has been given the "virtual" label (of course to be defined by each child). Note the TParentList would always hold TChild of some description.

Is this possible, and moreover is it smart?

 
Sorry, I don't get your question?
Do you mean polymorphism? (ie TAnimal - TMonkey
- TCat
- TDog)
or a real parent child relationship?
In the second case, the real question I would pose is: Does the parent need to know its children and/or vice versa?
Then indeed TList<T> would be a good candidate for that.

Maybe we need a more concrete example and build further on that? ;)

/Daddy

-----------------------------------------------------
Helping people is my job...
 
Well, your example actually is pretty good. Say, I have a TAnimal parent, and TCat, TDog, TMonkey as children. I'm asking if I define TAnimal with a virtual/abstract method named "walk", define that specifically in TCat and TDog, can I make a list of TAnimal consisting of all TCat, TDog, and TMonkey and iterate it and call the method "walk" in TAnimal and get what I'm wanting to happen.

Code:
  TAnimal = class
    procedure walk; virtual; abstract;
  end;
  TCat = class(TAnimal)
    procedure walk; override;
  end;
  TDog = class(TAnimal)
    procedure walk; override;
  end;
  TMonkey = class(TAnimal)
    procedure walk; override;
  end;

procedure TCat.Walk;
begin
  exit;
end;

procedure TDog.Walk;
begin
  exit;
end;

procedure TMonkey.Walk;
begin
  exit;
end;

var
  mylist: TAnimalList;

// basically can I do this, and if I can, is it smart?
for i := 0 to (MyList.Count - 1) do
  MyList.Items.Walk[i];


 
sure that is what polymorphism is all about. A more complete example can be found here.
Your list would be of type TList<TAnimal> (or TObjectList<TAnimal> if the list owns the objects).

/Daddy

-----------------------------------------------------
Helping people is my job...
 
Got it working, for most part. Related yet unrelated question.

On the animals, I don't need to provide all possible functions because the animals can't all do something, e.g.

TMonkey = class(TAnimal)
procedure ClimbTree; override;
end;

On TDog, providing such a function would be unnecessary, as dogs can't climb trees. Do I need to carry the ClimbTree definition through the whole hierarchy anyway (dummy procedures) to be able to do this:

for i := 0 to (MyList.Count - 1) do
MyList.Items.ClimbTree;

Or is there another way I'm missing?

 
Yes indeed.
Or you could make a intermediate class to categorize a bit.

Like :

Code:
TAnimalThatCanClimb = class(TAnimal)
public
 procedure ClimbTree; virtual; abstract;
end;

//then you monkey would become:

TMonkey = class(TAnimalThatCanClimb)
  procedure ClimbTree; virutal; override;
end;

// only downside is that you need to filter your list now:
for i := 0 to (MyList.Count - 1) do
 if (MyList.Items[i]).InheritsFrom(TAnimalThatCanClimb) then
   MyList.Items[i].ClimbTree;

-----------------------------------------------------
Helping people is my job...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top