Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > When to destroy, when not to destroy

Reply
Thread Tools

When to destroy, when not to destroy

 
 
Ook
Guest
Posts: n/a
 
      10-11-2005
I was taught that in a copy constructor, you don't have to destroy[] your
arrays, but in an overloaded assignment operator, you have to. Example:

When do you delete[], and when do you not? Is it arbitrary, or are there
general guidelines that should be followed? I'm thinking that in the copy
constructor, you are creating a new instance of the class, and in the
assignment, you have already created the class and therefore have to
destroy[] before you new. Is this correct?

// Copy constructor
_data = new int[ _size ];

// Overloaded Assignment operator:
delete [] _data;
_data = new int[_size];


 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      10-11-2005
I'm thinking that in the copy
constructor, you are creating a new instance of the class, and in the
assignment, you have already created the class and therefore have to
destroy[] before you new. Is this correct?

This is 100% correct. There is nothing to clean up in a copy
constructor.

The other thing you need to do in an assignment operator (and not in
any constructor) is to check for self assignment. In the above
example, if you don't check for this, you will have deleted your data!

 
Reply With Quote
 
 
 
 
Ook
Guest
Posts: n/a
 
      10-11-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm thinking that in the copy
> constructor, you are creating a new instance of the class, and in the
> assignment, you have already created the class and therefore have to
> destroy[] before you new. Is this correct?
>
> This is 100% correct. There is nothing to clean up in a copy
> constructor.
>
> The other thing you need to do in an assignment operator (and not in
> any constructor) is to check for self assignment. In the above
> example, if you don't check for this, you will have deleted your data!
>


Yeah, I got that - I omitted that part of the code for the sake of
simplicity. Glad I 'm on the right track


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Destroy a Singleton : static or not static ? requinham C++ 11 02-19-2010 08:12 AM
destroy primitive/object types but memory is not freed george972@mailinator.com C Programming 5 05-25-2009 10:44 PM
Help. Look for a software that can self-destroy a document file in a week and will not allow viewer to print CJ Computer Support 3 08-04-2006 01:10 PM
use delete to destroy primitive/object types but memory is not freed jimjim C Programming 28 04-13-2004 11:34 PM
references => how not to destroy my data ? Jan Perl Misc 4 07-08-2003 05:38 PM



Advertisments