Newbie Q: to point or not to point ?

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!
 
D

Dave Townsend

I would prefer to use method 2 in your posting since this is much simpler
and does "clean up" for you, but often this is
not possible because of the architecture/limitations of the GUI tool kit.
I would prefer to use "stack" based objects rather than allocating
with new since object cleanup is done automatically when the
object "goes out of scope". I'm not particularly familiar with the
wx toolkit, but other toolkits prevent widespread adoption of using
objects rather than pointers since in the API to the kits often need a
pointer to an object and that object remain a valid object and not be
destroyed prematurely. In addition, in some toolkits, parent widgets
will actually manage child widgets and delete/destroy them when they
are destroyed them selves, so its vital that a new'd widget be used in this
case.

Another issue is that in GUI toolkits, there is often a lot of polymorphism
going on, so its easier to create objects on the free store and pass around
pointers than to deal with actual objects. For instance, in the QT
toolkit,
you might subclass a Pushbutton widget, but you can substitute your derived
class object anywhere a pushbutton is required, since you are using a
pointer.

Widgets have a unique identity, that is to say, you create a button, that
button has its own state and individuality, but you might want to reference
that same button by pointer (or reference I suppose) from multiple places,
you
don't normally copy "widgets", that wouldn't make sense.

In your particular example, the dialog exists only for the duration of the
function call its created in, generally widgets are created and live for the
life of the application. In the dialog case, the creation of an actual
object
works fine but this mechanism is not so easy to apply in the general case.

dave
 
D

David White

USCode said:
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?

This question is not entirely on-topic here. In short, the answer depends on
whether the wxMessageDialog expects to instantiated with 'new', and we can't
answer that because this NG is for the discussion of standard C++ only (of
which wxWidgets is not a part). For example, if the Destroy member does
"delete this;" then you either need to instantiate with 'new' or not call
that function. Normally, you only use 'new' when you have to, so the second
version is preferred if it works. But you really need to find out the
correct way of using the wxWidgets library.

DW
 
J

John Harrison

Another issue is that in GUI toolkits, there is often a lot of
polymorphism
going on, so its easier to create objects on the free store and pass
around
pointers than to deal with actual objects. For instance, in the QT
toolkit,
you might subclass a Pushbutton widget, but you can substitute your
derived
class object anywhere a pushbutton is required, since you are using a
pointer.

That's not a valid reason to use new. Polymorphism works whether an object
is created on the free store or not.

john
 
U

USCode

David White said:
This question is not entirely on-topic here.

Sorry, I thought it was as I thought it would apply to C++ in general.
I'll post in the wxWidgets group.
 
C

crichmon

USCode said:
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?

First of all, I don't know much about the wxWidgets library, so I will try
and focus my comments on general C++.

In your first example, when you are done with dlgbx, you make a call to
Destroy(). Does Destroy() clean up the memory allocated from the earlier
new call? If not, you will need to include a delete statement when you are
done with the object (unless maybe if you are using C++.NET... does that
have garbage collection?):

wxMessageDialog* dlgbx = new wxMessageDialog(this, bgsString,
"Universal", wxYES_NO);
if (dlgbx->ShowModal() == wxID_YES)
{
// do something
}
dlgbx->Destroy();
delete dlgbx;

Given your examples, I would choose option #2. You only seem to want dlgbx
to be a local variable and exist for the life of the function, so there is
really no need to use a pointer and a call to new. Just create a normal
object and be done with it; it will be cleaned up when the function goes out
of scope. (Plus if you don't use new, you won't have to remember to use
delete when you're done with it.)

A pointer might be necessary if you create a wxMessageDialog as a local
variable and then decide to keep dlgbx around past the life of the
individual function call. for instance:

wxMessageDialog* 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();

return dlgbx;
}

The one drawback to this is that you will need to remember to delete the
wxMessageDialog that is returned elsewhere in the program, which can make
things not-so-neat. (Again, I'm speaking in general C++ terms, I'm not
completely aware of the details of and implications of using the wxWidgets
library.)

Besides the above example, pointers are useful if you have situations where
multiple things will need to 'reference' a single, specific object. If you
don't use pointers, you will have multiple copies of a single thing; it
would be better to have multiple pointers pointing to a single object in
memory. It will also save you memory space as, in most cases,
sizeof(wxMessageDialog) is greater > than sizeof(wxMessageDialog*) [the
latter of which is equivalent to sizeof(void*), which is usually equivalent
to sizeof(int) IIRC].

HTH,
crichmon
 
J

jeffc

USCode said:
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?

There really is no "advantage" to using pointer notation. You have to use
it when you use "new". When do you use "new"? Only when you have to
allocate things dynamically - for example, when you don't know ahead of time
how many object you'll need. Also, you might need to use a pointer of base
class type when actually pointing to a subclass type (polymorphism). Rule
of thumb: avoid using pointer notation until you find you can't get it to
work well without it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top