Re: How to "reset" an object
On Jun 21, 1:54 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Leigh Johnston" <le...@i42.co.uk> wrote in message
news:VqSdnUMbWKmfwoLRnZ2dnUVZ8oqdnZ2d@giganews.com...
Your function template is preferable to what the OP suggests
yes (yours is not a hack), however it might be desirable to
use destructors and constructors if for some reason you
don't want to write assignment operators for the class and
its members, although I cannot really think of a good reason
for not writing assignment operators if I'm honest.
The only reason I can think of for not wanting to write an
assignment operator is if the compiler generated one is
sufficient in which case assignment to a default constructed
object is sufficient for resetting anyway.
I'm not sure I've understood what you're saying correctly,
because I'm pretty sure you know this, but there are lots of
reasons why a class might not want to support assignment: most
polymorphic classes shouldn't, and of course, singletons won't.
Polymorphism is, of course, the basic reason why none of the
proposed solutions work. (The OP's solution, of course, will
cause problems even in the absense of polymorphism.) The
problem is that template type resolution is on the static type,
and to correctly reset an object, you need to act on the dynamic
type.
With regards to the original poster's problem, the easiest way
to reset an object to its initial state is to use a new object,
rather than the old one. I'm afraid I don't see much use for
his function, except in special cases. (It is sometimes useful
as an optimization technique to reuse a standard container. But
what makes it useful is that you don't completely reset it; you
keep any memory it has already allocated.)
--
James Kanze