By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

A question about "new" in the constructor argument?

P: n/a
Hi,

I saw the following code:

Composition *quick = new Composition(new SimpleCompositor);

SimpleCompositor and Composition are both classes.

My question is how to delete the newed SimpleCompositor object.

Best wishes,
Peng

Sep 1 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Pe*******@gmail.com wrote:
Hi,

I saw the following code:

Composition *quick = new Composition(new SimpleCompositor);

SimpleCompositor and Composition are both classes.

My question is how to delete the newed SimpleCompositor object.


That depends on the Composition class. It's the only thing that has a
pointer to it.
Sep 1 '05 #2

P: n/a
Pe*******@gmail.com wrote:
Hi,

I saw the following code:

Composition *quick = new Composition(new SimpleCompositor);

SimpleCompositor and Composition are both classes.

My question is how to delete the newed SimpleCompositor object.


Idiomatically, it seems like that responsibility is being assigned to
the Composition object. Presumably it deletes the SimpleCompositor in
its destructor or implicitly via a smart pointer (std::auto_ptr,
boost::scoped_ptr, etc.). If that is not the case, the Composition
object is very poorly designed and is prone to memory leaks.

As for the quick pointer, I would suggest using a smart pointer there
also to gain exception safety, guaranteed deletion, etc.

M

Sep 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.