By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Copy Constructor & assignment operator

P: n/a
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?

Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Jul 23 '05 #2

P: n/a
am*********@gmail.com wrote:
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
Jul 23 '05 #3

P: n/a
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?

Jul 23 '05 #4

P: n/a
am*********@gmail.com wrote:
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
Jul 23 '05 #5

P: n/a
am*********@gmail.com wrote:
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.

Jul 23 '05 #6

P: n/a

am*********@gmail.com wrote:
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

Jul 23 '05 #7

P: n/a
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);

// ...
}

--Paul

Jul 23 '05 #8

P: n/a

<am*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Jul 23 '05 #9

P: n/a
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

Jul 23 '05 #10

P: n/a
<am*********@gmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.