Board index » cppbuilder » TForm constructor but where is the destructor?

TForm constructor but where is the destructor?

When i make a new TForm i get the constructor automatically but i dont
get a destructor.

If i create a variable with new in the constructor i want to delete it in
the destructor. Can i just supply my own destructor code?

// ---- Example start ----------------------------
__fastcall TMyForm(TComponent* Owner)
    : TForm(Owner)
{
    pVar = new TSomeClass;

Quote
}

__fastcall ~TMyForm(TComponent* Owner)
    : TForm(Owner)
{
    delete pVar;
Quote
}

// ---- Example end ----------------------------
 

Re:TForm constructor but where is the destructor?


Quote
"Mats G?rdstad Friberg" wrote:

> When i make a new TForm i get the constructor automatically but i dont
> get a destructor.

> If i create a variable with new in the constructor i want to delete it in
> the destructor. Can i just supply my own destructor code?

> // ---- Example start ----------------------------
> __fastcall TMyForm(TComponent* Owner)
>     : TForm(Owner)
> {
>     pVar = new TSomeClass;
> }

> __fastcall ~TMyForm(TComponent* Owner)
>     : TForm(Owner)
> {
>     delete pVar;
> }
> // ---- Example end ----------------------------

Absolutely!

Note:  the __fastcall *is* required as you are overriding the virtual
destructor of the base class, which is in turn declared __fastcall.

An alternative might appear to be the OnCreate/OnDestroy events, but
there have been numerous postings over the years why these don't work as
well in BCB as they do in Delphi (due to different
construction/destruction orders between the languages).

The only thing I might suggest as a further refinement would be to store
TSomeClass as some kind of auto_ptr for better resource protection.
Bsaically, if your constructor throws an exception, your destructor is
not called, but if you wrap the pointer in a smart-pointer class, and
construct it in your initialiser list, the smart-pointer destructor will
be called, even though your form class destructor isn't.

AlisdairM

Re:TForm constructor but where is the destructor?


"AlisdairM" <alisdair.meredith@NO_SPAM_PLEASE.benettonformula.com> wrote in
message news:39DAFE8D.5A782D73@NO_SPAM_PLEASE.benettonformula.com...

Quote
> "Mats G?rdstad Friberg" wrote:

> > When i make a new TForm i get the constructor automatically but i dont
> > get a destructor.

> > If i create a variable with new in the constructor i want to delete it
in
> > the destructor. Can i just supply my own destructor code?

> > // ---- Example start ----------------------------
> > __fastcall TMyForm(TComponent* Owner)
> >     : TForm(Owner)
> > {
> >     pVar = new TSomeClass;
> > }

> > __fastcall ~TMyForm(TComponent* Owner)
> >     : TForm(Owner)
> > {
> >     delete pVar;
> > }
> > // ---- Example end ----------------------------

When you create the Dtor, you need the __fastcall but you shouldn't have any
args in the arg list, nor should you be declaring it as derived from TForm.
A Dtor is automatically called when the object is destroyed.  It doesn't
need to have an owner specified.  I believe that both of these will give
compile errors at any rate.

Try simply:   __fastcall ~TMyForm();

Re:TForm constructor but where is the destructor?


Quote
Duane Hebert wrote:
> When you create the Dtor, you need the __fastcall but you shouldn't have any
> args in the arg list, nor should you be declaring it as derived from TForm.
> A Dtor is automatically called when the object is destroyed.  It doesn't
> need to have an owner specified.  I believe that both of these will give
> compile errors at any rate.

> Try simply:   __fastcall ~TMyForm();

Oops, my bad!  Not reading clearly today :? (

Thanks for the long-stop Duane.

AlisdairM

Re:TForm constructor but where is the destructor?


Mats G?rdstad Friberg <mats.g.frib...@datarutin.se> writes:

Quote
> When i make a new TForm i get the constructor automatically but i dont
> get a destructor.

> If i create a variable with new in the constructor i want to delete it in
> the destructor. Can i just supply my own destructor code?

Yes, but destructors do not take any parameters.  

Quote
> __fastcall ~TMyForm(TComponent* Owner)
>     : TForm(Owner)
> {
>     delete pVar;
> }

--
Chris (TeamB);

Other Threads