I'd like to use a method of a class without assigning its creation to a variable. For example:
type
TMyObject = class
public
procedure DoSomething;
end;
implementation
procedure MyObjDoesSomething;
begin
TMyObject.Create.DoSomething;
end;
{ TMyObject }
procedure TMyObject.DoSomething;
begin
// ...
end;
As you can see, in MyObjDoesSomething
I didn't assign TMyObject.Create
to a TMyObject
variable.
Does this have some kind of impact on memory usage, or even something else I did't think of?
If you
Create
an object to allocate memory for it, you must callFree()
on the object when you are done using it to release that memory, otherwise the memory is leaked, which will impact memory over time if you create these objects multiple times.If you don't want to use an explicit variable to refer to the object (why?), you can use a
with
block, eg:Another option is to make the class implement an
interface
, and then you can let the compiler's reference counting of interfaces handle freeing the object for you. This has a little bit more overhead than above, but you don't have to worry as much about handling the memory manually, eg:But really, just use a variable instead. It costs you nothing to use: