Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy constructor

Reply
Thread Tools

copy constructor

 
 
brekehan
Guest
Posts: n/a
 
      01-18-2007
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());
}

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      01-18-2007

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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-19-2007
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).

 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments