copy constructor

Discussion in 'C++' started by brekehan, Jan 18, 2007.

  1. brekehan

    brekehan Guest

    Do you need to release any resources that have been allocated in for
    object's member data before assigning new data in a copy constructor?


    Bleh & Bleh(const Bleh rhs)
    {
    // Is this needed? Can a copy constructor ever get called on an
    existing instance of an object?
    delete bleh_member;

    bleh_member = new Something();
    bleh_member->SetData(rhs.bleh_member->(GetData());
    }
    brekehan, Jan 18, 2007
    #1
    1. Advertising

  2. brekehan wrote:
    > Do you need to release any resources that have been allocated in for
    > object's member data before assigning new data in a copy constructor?


    No. It sounds like you're confusing the copy constructor - which
    constructs a _new_ object - with the assignment operator, aka
    operator=(), which operates on an existing object.

    >
    > Bleh & Bleh(const Bleh rhs)
    > {
    > // Is this needed? Can a copy constructor ever get called on an
    > existing instance of an object?


    No.

    > delete bleh_member;
    >
    > bleh_member = new Something();
    > bleh_member->SetData(rhs.bleh_member->(GetData());
    > }


    Best regards,

    Tom
    Thomas Tutone, Jan 18, 2007
    #2
    1. Advertising

  3. brekehan

    Rolf Magnus Guest

    brekehan wrote:

    > Do you need to release any resources that have been allocated in for
    > object's member data before assigning new data in a copy constructor?
    >
    >
    > Bleh & Bleh(const Bleh rhs)


    This is quite a bit off if it's supposed to be a copy constructor.
    Constructors don't return anything, and a copy constructor must take a
    reference.

    > {
    > // Is this needed? Can a copy constructor ever get called on an
    > existing instance of an object?


    No. Constructors construct new objects. They are never called on existing
    objects (at least not in a correct program).
    Rolf Magnus, Jan 19, 2007
    #3
    1. Advertising

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. VisionSet
    Replies:
    8
    Views:
    4,893
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    463
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    572
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,221
  5. cinsk
    Replies:
    35
    Views:
    2,600
    James Kanze
    Oct 11, 2010
Loading...

Share This Page