U
USCode
New to C++ and am also trying to utilize the wxWidgets library.
I'm still on the 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. (I apologize
if my terminology is incorrect)
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" operators 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 C++ in general, what are the advantages and/or disadvantages to these
2 methods of instantiating the object with and without the "new" operator?
Thanks!
I'm still on the 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. (I apologize
if my terminology is incorrect)
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" operators 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 C++ in general, what are the advantages and/or disadvantages to these
2 methods of instantiating the object with and without the "new" operator?
Thanks!