A question about "new" in the constructor argument?

P

PengYu.UT

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
 
R

Rolf Magnus

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.
 
M

mlimber

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top