When to destroy, when not to destroy

Discussion in 'C++' started by Ook, Oct 11, 2005.

  1. Ook

    Ook Guest

    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];
     
    Ook, Oct 11, 2005
    #1
    1. Advertisements

  2. Ook

    Guest

    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!
     
    , Oct 11, 2005
    #2
    1. Advertisements

  3. Ook

    Ook Guest

    <> wrote in message
    news:...
    > 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 ;)
     
    Ook, Oct 11, 2005
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    Block in Perl/TK MainWindow destroy

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    0
    Views:
    731
    Cyril Vi?ville
    Jun 29, 2004
  2. Jenny

    Destroy COM object

    Jenny, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    754
    Alvin Bruney
    Jul 21, 2003
  3. Jenny

    Destroy COM object

    Jenny, Jul 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    621
  4. jimjim
    Replies:
    28
    Views:
    1,075
    Michael Wojcik
    Apr 14, 2004
  5. Replies:
    5
    Views:
    750
    James Kuyper
    May 25, 2009
  6. requinham
    Replies:
    11
    Views:
    2,904
    Michael Doubez
    Feb 19, 2010
  7. Jan
    Replies:
    4
    Views:
    221
  8. Eli
    Replies:
    0
    Views:
    212
Loading...