Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: Another Pointer Question - TObjectList

  1. #11

    Another Pointer Question - TObjectList

    cairnswm is right, classes are already pointers so there is no need to declare a Pointer type for them. So by declaring a pointer type, you are essentially creating a Pointer to a Pointer.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #12

    Another Pointer Question - TObjectList

    Quote Originally Posted by cairnswm
    I must say that I think you've missed the point of classes. In Delphi any variable of a class type is already a pointer. There is no reason to have a PType as a pointer to the class as you are effectivly creating a pointer to a pointer to the class instance. This PType structure basically is legacy from Turbo Pascal and should not be used anymore.
    This is exacly why i always tell around not to use pointers
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  3. #13

    Another Pointer Question - TObjectList

    Quote Originally Posted by savage
    cairnswm is right, classes are already pointers so there is no need to declare a Pointer type for them. So by declaring a pointer type, you are essentially creating a Pointer to a Pointer.
    OK, so if I want to have a variable called Player that is actually an entry in the TObjectList, how do I do that?

    Thanks
    http://www.c5software.co.uk (site is being developed at the moment)

  4. #14
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Another Pointer Question - TObjectList

    (Not tested in Delphi)

    [pascal]
    Type
    TItem = Class
    <properties>
    End;
    var
    Items : TList;

    Procedure Create;
    Var
    I : TItem;
    Begin
    Items := TList.Create;
    I := TItem.Create;
    I.Init;
    Items.Add(I);
    End;

    Function GetItem(Index : Integer) : TItem;
    Begin
    Result := TItem(Items[Index]);
    End;

    Player := GetItem(0);
    [/pascal]
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  5. #15
    Anonymous
    Guest

    Another Pointer Question - TObjectList

    Quote Originally Posted by "cairnswm
    The only time you need PTypes in delphi is if you are using records and want to put them into something like an object list.
    yeah, or linked lists, or if one want to access memory address directly, if I'm not wrong, the purpose of OOP is to relieve the programmer of using pointers; however; I see myself using them quite a lot..

  6. #16
    Anonymous
    Guest

    Another Pointer Question - TObjectList

    Quote Originally Posted by "cairnswm
    The only time you need PTypes in delphi is if you are using records and want to put them into something like an object list.
    yeah, or linked lists, or if one want to access memory address directly, if I'm not wrong, the purpose of OOP is to relieve the programmer of using pointers; however; I see myself using them quite a lot..but in a strict sense they should be avoided.

  7. #17
    Anonymous
    Guest

    Another Pointer Question - TObjectList

    [quote="Anonymous"]
    Quote Originally Posted by cairnswm
    The only time you need PTypes in delphi is if you are using records and want to put them into something like an object list.
    yeah, or ]

  8. #18

    Another Pointer Question - TObjectList

    I tend to like things more strongly typed so I would use something like
    [pascal]
    interface

    type
    TCreature = class( TObject )
    // General Creature features
    end;

    TSpecialistCreatureA = class( TObject )
    // SpecialistCreatureA features
    end;

    TSpecialistCreatureB = class( TObject )
    // SpecialistCreatureB features
    end;

    TPlayer = class(TCreature)
    // Specialise my Creature so it behaves more like a Player
    end;

    TCreatureManager = class( TObjectList )
    protected
    function GetItem(Index: Integer): TCreature;
    procedure SetItem(Index: Integer; AObject: TCreature);
    public
    function Extract(Item: TCreature): TCreature;
    function First: TCreature;
    function Last: TCreature;
    property Items[Index: Integer]: TCreature read GetItem write SetItem; default;
    end;

    implementation

    { TCreatureManager }
    function TCreatureManager.Extract(Item: TCreature): TCreature;
    begin
    result := TCreature(inherited Extract(Item));
    end;

    function TCreatureManager.Last: TCreature;
    begin
    result := TCreature(inherited Last);
    end;

    function TCreatureManager.GetItem(Index: Integer): TCreature;
    begin
    result := TCreature(inherited Items[Index]);
    end;

    function TCreatureManager.First: TCreature;
    begin
    result := TCreature(inherited First);
    end;

    procedure TCreatureManager.SetItem(Index: Integer; AObject: TCreature);
    begin
    inherited Items[Index] := AObject;
    end;

    end.
    [/pascal]

    This does compile in Delphi.

    If you really need to, you could also override the Add, Insert and other methods within the TObjectList decendant if you need to do some special things when adding etc. But as it is this code should handle managing a list of creatures and their decendents.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  9. #19

    Another Pointer Question - TObjectList

    That code looks solid.
    OOP at it's best!
    Marmin^.Style

  10. #20
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Another Pointer Question - TObjectList

    I agree on the specialised objects.

    For Run-A-War I think there are 17 different classes. Lots of classes only really helps if you use polymorphism properly.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •