copy constructor

B

brekehan

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());
}
 
T

Thomas Tutone

brekehan said:
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
 
R

Rolf Magnus

brekehan said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top