INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

List of Descendent Classes

List of Descendent Classes

(OP)
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?

RE: List of Descendent Classes

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...

RE: List of Descendent Classes

(OP)
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]; 

RE: List of Descendent Classes

(OP)
Should be MyList.Items[i].Walk; (an edit)

RE: List of Descendent Classes

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...

RE: List of Descendent Classes

(OP)
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[i].ClimbTree;

Or is there another way I'm missing?

RE: List of Descendent Classes

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...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close