Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #15356
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: memory allocation/deaalocation in calss |
| Date | 2012-04-13 23:35 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <jma66g$pmp$1@dont-email.me> (permalink) |
| References | <28714271.3.1334350972034.JavaMail.geo-discussion-forums@pbcuv5> |
On 13.04.2012 23:02, asit wrote:
> I have a LinkedList class which has a function which allocates memory.
>
> I need to add user defined copy constructor and assignment operator.
>
> In case of assignment operator, I need to free the existing memory
> allocated(which is done by my destructor). Should I call destructor
> inside that assignment operator body ?
You should only explicitly call a destructor for an object constructed
in-place in existing storage (if that doesn't ring any bell, then it
means just don't explicitly call any destructor).
For an assignment operator, unless there are good reasons otherwise, use
the idiomatic implementation in terms of copy construction,
void operator=( MyList other ) { swapWith( other ); }
where swapWith is a MyList member function that swaps the contents of
the object that it's called on, with the contents of another such
object, guaranteed without failing.
It's simple and safe, and you then just need to implement custom copy
construction and the swapWith method.
Cheers & hth.,
- Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 14:02 -0700
Re: memory allocation/deaalocation in calss "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-13 23:35 +0200
Re: memory allocation/deaalocation in calss Ian Collins <ian-news@hotmail.com> - 2012-04-14 09:36 +1200
Re: memory allocation/deaalocation in calss Kevin McCarty <kmccarty@gmail.com> - 2012-04-13 14:35 -0700
Re: memory allocation/deaalocation in calss Juha Nieminen <nospam@thanks.invalid> - 2012-04-14 05:27 +0000
Re: memory allocation/deaalocation in calss Christopher Creutzig <christopher@creutzig.de> - 2012-04-26 19:46 +0200
Re: memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 15:17 -0700
Re: memory allocation/deaalocation in calss Ian Collins <ian-news@hotmail.com> - 2012-04-14 11:29 +1200
Re: memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 15:31 -0700
csiph-web