when to use "new"

D

DaKoadMunky

Such an object has it's lifetime managed by the programmer. What's your

I interpreted your statement to mean that one should *only* use new for the
purposes of managing an objects lifetime.

I suggest that another purpose for using new is to defer the decision as to
which type of object to create to run-time.

It is true that the object will have its lifetime managed by the programmer but
the responsibility for lifetime management might be a consequence rather than a
goal.

void DoSomething(const string& typeIndicator)
{
Base* base = SomeFactoryFunction(typeIndicator);

delete base;
}

In this case the lifetime of the object presumably created with new is
identical to that of a variable with automatic storage duration. The need to
explicitly manage the lifetime of the object seems to be a consequence that
resulted from the need to delay the decision as to which kind of object to
create until run-time.

My only point is that lifetime management in one case might be exactly what is
desired whereas in another case it might just be a "side-effect".
 
J

Jason Heyes

DaKoadMunky said:
I interpreted your statement to mean that one should *only* use new for
the
purposes of managing an objects lifetime.

I suggest that another purpose for using new is to defer the decision as
to
which type of object to create to run-time.

It is true that the object will have its lifetime managed by the
programmer but
the responsibility for lifetime management might be a consequence rather
than a
goal.

void DoSomething(const string& typeIndicator)
{
Base* base = SomeFactoryFunction(typeIndicator);

delete base;
}

In this case the lifetime of the object presumably created with new is
identical to that of a variable with automatic storage duration. The need
to
explicitly manage the lifetime of the object seems to be a consequence
that
resulted from the need to delay the decision as to which kind of object to
create until run-time.

My only point is that lifetime management in one case might be exactly
what is
desired whereas in another case it might just be a "side-effect".

I'm afraid you've lost me. I need a practical example of how a pointer could
be used without making it necessary to manage an object's lifetime. Dynamic
typing does make it necessary so as an example it isn't good enough.
 
M

Michiel Salters

What if the type of object to be created is not known until run-time?

True, you need a pointer or reference there. A reference is often
a solution if you need either one of N possible globals.

i.e.
base& get1() { static der1 r; return r; }
base& get2() { static der2 r; return r; }
base& the_chosen = runtime_cond() ? get1() : get2();
What if the number of objects to be created is not known until run-time?

At the lowest level I believe the new operator would be required.

It's often used, including array new[], and placement new(here). But
the first example shows there are alternatives. And even where you
use new, it's bet used indirectly - e.g. as part of a std:: class.

BTW, even if you use new directly, avoid the need for a matching
delete. Use a smart pointer instead, e.g. boost::scoped_ptr.

Regards,
Michiel Salters
 
R

Richard Herring

Jason Heyes said:
I'm afraid you've lost me. I need a practical example of how a pointer could
be used without making it necessary to manage an object's lifetime.

Easy. Make it point to something that's not on the heap.

class Base { /*...*/};
class D1: public Base{/*...*/};
class D2: public Base{/*...*/};
/* etc. */

void DoSomething(int typeIndicator)
{
D1 d1;
D2 d2;
/* etc. */
Base * p=0;
switch (typeIndicator)
{
case 1: p = &d1; break;
case 2: p = &d2; break;
/*...*/
}
assert(p);
p->doSomething();

// note the absence of "delete p;"
}
 
R

RCS

BTW, even if you use new directly, avoid the need for a matching
delete. Use a smart pointer instead, e.g. boost::scoped_ptr.

Regards,
Michiel Salters

Or, roll your own smart pointer class, which is a trivial thing to do,
with the benefit of totally controlling what's in the smart ptr class
(behaivour, etc), and without the hassle of importing another library
into the already muddled mix of libraies one have to deal with.

RCS
 

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
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top