PDA

View Full Version : Overriding Create event



dazappa
25-04-2010, 08:09 PM
Basically, I have a nice and simple object such as

TObj = class
public
x,y: integer;
end;

And I do this to create the object:

var
myObj: TObj;
myObj := TObj.Create;


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

procedure Create; override;
...
procedure TObj.Create;
begin
x := 0;
y := 0;
inherited;
end;

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.

AthenaOfDelphi
25-04-2010, 08:16 PM
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.

dazappa
25-04-2010, 08:24 PM
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?

AthenaOfDelphi
25-04-2010, 08:47 PM
In Delphi, you would do this:-



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

...

constructor TMyObj.create;
begin
inherited;

// My code here
end;


You don't need override on the create.

chronozphere
25-04-2010, 10:10 PM
If you are deriving from TObject, It's not neccesary to call Inherited;


{************************************************* ***************************
TOBJECT
************************************************** **************************}

constructor TObject.Create;

begin
end;

destructor TObject.Destroy;

begin
end;


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

Andreaz
26-04-2010, 05:08 AM
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.