Results 1 to 6 of 6

Thread: Overriding Create event

  1. #1

    Overriding Create event

    Basically, I have a nice and simple object such as
    [pascal]
    TObj = class
    public
    x,y: integer;
    end;[/pascal]

    And I do this to create the object:
    [pascal]
    var
    myObj: TObj;
    myObj := TObj.Create;
    [/pascal]

    And basically, I want to be able to call my own code as well as the inherited code when .Create is called.

    I tried adding
    [pascal]
    procedure Create; override;
    ...
    procedure TObj.Create;
    begin
    x := 0;
    y := 0;
    inherited;
    end;
    [/pascal]
    But it said: villager.pas(17,21) Error: There is no method in an ancestor class to be overridden: "TVillager.Create;"

    FPC 2.4.0, Lazarus .9.28.3. (Shouldn't matter, but eh).

    I can work around this if it's not possible (or I don't receive a response in time), but it's going to be ugly.

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    Re: Overriding Create event

    I'm not big on FPC, but could it be as simple as the lack of a parent class?

    You've defined TObj as Class, try TObj = class(TObject).

    AS I say, I'm not big on FPC, but thats the only thing I could think of from looking at your code.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3

    Re: Overriding Create event

    Quote Originally Posted by AthenaOfDelphi
    I'm not big on FPC, but could it be as simple as the lack of a parent class?

    You've defined TObj as Class, try TObj = class(TObject).

    AS I say, I'm not big on FPC, but thats the only thing I could think of from looking at your code.
    Still doesn't like me tacking on override; to that. If I take off override, and still try to do
    myObj := TObj.Create
    It shows
    "Error: Only class methods can be referred with class references"

    I think I might have to go the uglier way then, eh?

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    Re: Overriding Create event

    In Delphi, you would do this:-

    [pascal]

    TMyObj = class(TObject)
    public
    constructor create;
    end;

    ...

    constructor TMyObj.create;
    begin
    inherited;

    // My code here
    end;
    [/pascal]

    You don't need override on the create.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5

    Re: Overriding Create event

    If you are deriving from TObject, It's not neccesary to call Inherited;

    [pascal]
    {************************************************* ***************************
    TOBJECT
    ************************************************** **************************}

    constructor TObject.Create;

    begin
    end;

    destructor TObject.Destroy;

    begin
    end;
    [/pascal]

    As you see, both the constructor and the Destructor don't contain anything, so there's no reason to do it (This is source as used by FPC and Lazarus).

    However, I feel that it's good practice to do so. If you ever decide to let your class derive from something else, the "Inherited" calls DO matter. I can call inherited in the constructor of TObject-descendants without any problems (In delphi), so I do it all the time.

    The problem might be caused by the fact that TObject.Create is not virtual. Only virtual methods may be overridden. It's pretty weird that this isn't mentioned in the error. It probably should be something like "Cannot override. TObject.Create is not virtual".

    In the end, I'd just leave it out for TObject-descendants. If you ever want to derive class B from class A, you should make A.Create virtual, so you can override it in B.

    Hope this helps.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #6

    Re: Overriding Create event

    In Delphi, if you do the same you got an error about not being able to override a non virtual method, but then FPC gives a lot of strange compilation errors, not that I'm complaining, getting a lot more then we're paying for.

    So the correct solution is as mention is not to override the constructor.
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

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
  •