Copy Constructor & assignment operator

A

ambar.shome

Dear All,

Whats the difference between a copy constructor and assignment
operator. We can assign the values of member variables of one object to
another object of same type using both of them. Then where is the
difference?
 
P

Paul

Dear All,

Whats the difference between a copy constructor and assignment
operator. We can assign the values of member variables of one object to
another object of same type using both of them. Then where is the
difference?

The copy constructor is used to initialize a new instance from an old
instance, and is called when passing variables by value into functions
or as return values out of functions.
The assignment operator is used to change an existing instance to have
the same values as the rvalue, which means that the instance has to be
destroyed and re-initialized if it has internal dynamic memory.

You should look for examples and gotchas if you're trying to implement
them, see what the default implementation does (provided by the
compiler) and also understand why these are sometimes made private.
--Paul
 
B

blueblueblue2005

I guess they are just for the convenience that you could create the
objects in different ways. for example, suppose A is class with
constructor A(int) and A(const A)

A a1(5);
A a2(a1);
A a3 = a2; // you can always create a3 using A a3(a2)

but suppose you have already create a3 using A a3(10), then you want to
assign a1 or a2 to a3, you have to use assignment operator: a3 = a1
 
A

ambar.shome

Thanks for the reply. I understood the difference between Copy Cons and
Assignment Operator. But, I could not make out when should I use copy
constructor and when should I use assignment operator. Can anyone
enlighten me on that?
 
P

Peter Julian

Dear All,

Whats the difference between a copy constructor and assignment
operator. We can assign the values of member variables of one object to
another object of same type using both of them. Then where is the
difference?

One creates an object (copy ctor) based on another's attributes and the
other essentially modifies an object.

as in:

int x(5);
x = 6;

The trick here is to recognize that you have the option and the ability to
define how the copy-creation or assignment occurs. This can be rather
important with complex types which manage their own allocations, for
example.
 
P

Paul

Thanks for the reply. I understood the difference between Copy Cons and
Assignment Operator. But, I could not make out when should I use copy
constructor and when should I use assignment operator. Can anyone
enlighten me on that?

As with about anything else, it depends on what you are doing.
If you want to make a copy of an instance, the copy constructor does it
in one step instead of creating a default instance and then copying members.
Initializing an instance where it is declared with the equals operator
can be compiled to use the copy constructor, I think. It may depend on
the compiler.

class A; // defined elsewhere
void f()
{
A a1; // initialize an A with default constructor
A a2(a1); // construct a new A using a1's values
A a3; // construct a default A
a3 = a1; // then replace values. Potentially more expensive
// than using copy ctor.
A a4 = a1; // should resolve to using copy ctor, but I am not sure.

// ...
}

--Paul
 
G

Greg

Thanks for the reply. I understood the difference between Copy Cons and
Assignment Operator. But, I could not make out when should I use copy
constructor and when should I use assignment operator. Can anyone
enlighten me on that?

In general, and even though it may seem counterintuitive, you should
prefer construction over assignment, whenever a choice is available.

For example, consider the numberString variable in the following
routine:

void PrintOneThroughTen()
{
for (int i = 1; i < 11; i ++)
{
std::string numberString( std::atoi( i ));

std::cout << numberString << ", ";
}
}

and in this implementation:

void PrintOneThroughTen()
{
std::string numberString;

for (int i = 1; i < 11; i ++)
{
numberString = std::atoi( i );

std::cout << numberString << ", ";
}
}

The first implementation constructs numberString while the second
assigns it a value each time through the loop. Both examples are
correct, but the first one by using construction is the more efficient
implementation than the second one which uses assignment to ensure that
numberString has the intended value.

The explanation for this rule of thumb (construction instead of
assignment) is left as an assignment to the reader to provide. :)

Greg
 
R

Rolf Magnus

Thanks for the reply. I understood the difference between Copy Cons and
Assignment Operator. But, I could not make out when should I use copy
constructor and when should I use assignment operator. Can anyone
enlighten me on that?

If you know the difference between them, you should know when to use each of
them. If you want to make a new object that is a copy of an existing one,
create it using the copy constructor. If you already have the object and
want to overwrite its contents to make it a copy of another object, use the
assignment operator.
 
B

benben

class A; // defined elsewhere
void f()
{
A a1; // initialize an A with default constructor
A a2(a1); // construct a new A using a1's values
A a3; // construct a default A
a3 = a1; // then replace values. Potentially more expensive
// than using copy ctor.
A a4 = a1; // should resolve to using copy ctor, but I am not sure.

The above line is ALWAYS a copy construction, which is just a short hand
for:

A a5 = A(a1);
 
M

Me

Whats the difference between a copy constructor and assignment
operator. We can assign the values of member variables of one object to
another object of same type using both of them. Then where is the
difference?

Constructors are for initializing memory. Copy constructors are for
intializing it to another value of the same type (ignoring cv
qualifications). Assignment operators are for assigning an initialized
variable to another value. For example:

A foo;
A boo(foo); // copy ctor
A goo = A(foo);
// create temporary using a copy ctor, copy construct
// temporary into goo. Most compilers can elide this
// temporary due to RVO.
A moo = boo;
// same as A moo(boo); not A moo = A(boo); because
// boo is the same type as moo.
boo = foo; // assignment operator
boo.~A(); // destructor
// boo = foo;
// error: don't use assignment operator since boo is
// destroyed now
::new ((void*)&boo) A(foo);
// ok: copy constructs using placement new
// ::new ((void*)&boo) A(foo);
// error: don't construct an already initialized variable
 
H

Heinz Ozwirk

Thanks for the reply. I understood the difference between Copy Cons and
Assignment Operator. But, I could not make out when should I use copy
constructor and when should I use assignment operator.

You should never "use" them. The compiler needs them to do its work. All you
have to do is implementing both of them (and a d'tor) if any one of them
must do something the compiler-generated versions wouldn't do.

HTH
Heinz
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top