delphi - How do I specify that my class be the owner of an object it creates? -
i trying define class have public adoconnection app can set.
however, cannot constructor working create adoconnection variable. code have far:
unit superheroclass; interface uses adodb; type tsuperhero = class private myqry: tadoquery; constructor create; public mycon: tadoconnection; end; implementation constructor tsuperhero.create; begin mycon := tadoconnection.create(self); end; end. if not mistaken, need create these internal class variables using self belong class, , can free them in class destructor.
this code gives me error:
[error] superheroclass.pas(23): incompatible types: 'tcomponent' , 'tsuperhero'
what doing wrong here?
you declare tsuperhero = class(tcomponent).
a tcomponent ancestor has ability own , manage other components
also make sure constructor public if want visible other units. i.e.:
public constructor create(aowner: tcomponent); override; ... constructor tsuperhero.create(aowner: tcomponent); begin inherited create(aowner); // tsuperhero.self owns mycon , responsible freeing mycon := tadoconnection.create(self); end; if superhero needed across application, instantiate tsuperhero like:
superhero := tsuperhero.create(application); and let application owner of superhero responsible freeing it.
just add answer, class tsuperhero usage tdatamodule (tcomponent), maybe should use centralized tdatamodule - drop tadoconnection on it, , on, , use instance across application.
Comments
Post a Comment