I was wondering if it is possible to have an object instance without calling any constructor directly. Something like that:
var
aObject : TMyObject;
begin
aObject.MyMethod; //will cause an AV, but is it possible?
end;
Edit:
I know about static methods, but thats is not what i am looking for. I am looking for an way to get the constructor called without having to explicit call it.
Delphi's objects are all heap-allocated: you can't simply declare an object and call methods on it, like you could do in C++ for example, you have to call an constructor to allocate and set up memory. But note even in C++ you are actually calling an constructor when you do that!
Maybe you can get away with a record and not an object? Example:
type
TMyObject = record // name intentionally left as in OP's code
procedure MyMethod;
end;
procedure TMyObject.MyMethod;
begin
// Do something
end;
// Use example:
procedure Test;
var MyObject: TMyObject; // TMyObject is a record so it is stack-allocated
begin
MyObject.MyMethod; // Works.
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With