f



To "new" or not to "new" ...

Brand new to C++ and to wxWidgets as well...
I'm still on the steep learning curve for both and for some reason it's not
clear
to me what the advantages/disadvantages to simply instantiating an object or
creating a pointer to that new object with the "new" operator.
Consider these 2 examples:

(1)
void Universal::OnToolClick( wxCommandEvent& event )
{
    wxString bgsString;
    bgsString = wxGetOsDescription();
    wxMessageDialog* dlgbx = new wxMessageDialog(this, bgsString,
"Universal", wxYES_NO);
     if (dlgbx->ShowModal() == wxID_YES)
      {
        // do something
      }
     dlgbx->Destroy();
    event.Skip();
}

(2)
void Universal::OnToolClick( wxCommandEvent& event )
{
    wxString bgsString;
    bgsString = wxGetOsDescription();
    wxMessageDialog dlgbx(this, bgsString, "Universal", wxYES_NO);
     if (dlgbx.ShowModal() == wxID_YES)
      {
        // do something
      }
    dlgbx.Destroy();
    event.Skip();
}

In example 1, the wxMessageDialog object dlgbx is instantiated with the
"new" operator and a pointer created to that object.  I then need to use
the -> operator to utilize that object's methods.
In example 2, the object is instantiated without the new operator and I can
then use dot-notation to utilize the object's methods.

Q: In wxWidgets, what are the advantages and/or disadvantages to these
2 methods of instantiating the object with and without the "new" operator?
All the examples seem to use example 1 method.

Q: In the 2nd example, do I even need the dlgbx.Destroy() method call?
Also, this method is more intuitive to me (newbie) and I think object will
be cleaned
up for me automatically when the method is exited, correct?  But will this
interfere with the way wxWidgets was designed to work?

Thanks!



0
USCode
7/8/2004 11:28:09 PM
comp.soft-sys.wxwindows 21076 articles. 0 followers. Post Follow

0 Replies
787 Views

Similar Articles

[PageSpeed] 12

Reply: