'C1 = C2' == Copy Constructor

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

Hi,

What is the difference between assignment operator and Copy
constructor.

I got the following info, But have some quries based on it.

Circle C1,C2;
C1 = C2; /* Assignment Operator */
Circle C3 = C2; /*Copy Constructor*/

In Above example, It states that for Copy Constructor, the compiler
creates a new object C3 and and copies the data from C2 member-by-
meber, into C3.
It also states that the Assignment operator does not create an object.

But How ? Actually 'Circle C1' has already been done before doing 'C1
= C2'.
So, how does it state the 'Assignment operator does not create an
object' ?

What actually is the difference between the assignment operator and
the copy constructor ?

Thx in advans,
Karthik Balaguru
 
D

deepakvsoni

Copy constructor is called when you declare and define an object.
Assignment operator is called when you just assign to an object that
is already constructed.
Assignment operator just assigns the member of the object on the right
to the members of the object on the left.
i.e in your case C1 = C2 is assignment and it does member wise
assignment.

but Circle C3 = C2 is converted to C3(C2) which is your copy
constructor. Here you're creating a new object and initializing (if
you initialize in initializer list) or assigning if you put statements
in { }
ex:
Circle {
public:
/* constructor and other functions */
/* copy constructor */
/* Here memory for radius is allocated and is initialized
with obj.radius */
// Circle(const Circle &obj):radius(obj.radius) { }

/* copy constructor which is assigning values */
/* Here the object is allocated memory and radius has some
value. you're then altering the value of radius by assigning to it
the obj.radius */
Circle(cosnt Circle &obj) { radius = obj.radius; }
private:
int radius;
};
 
N

Neelesh Bodas

Copy constructor is called when you declare and define an object.
Assignment operator is called when you just assign to an object that
is already constructed.
Assignment operator just assigns the member of the object on the right
to the members of the object on the left.
i.e in your case C1 = C2 is assignment and it does member wise
assignment.

but Circle C3 = C2 is converted to C3(C2) which is your copy
constructor. Here you're creating a new object and initializing (if
you initialize in initializer list) or assigning if you put statements
in { }
ex:
Circle {
public:
/* constructor and other functions */
/* copy constructor */
/* Here memory for radius is allocated and is initialized
with obj.radius */
// Circle(const Circle &obj):radius(obj.radius) { }

/* copy constructor which is assigning values */
/* Here the object is allocated memory and radius has some
value. you're then altering the value of radius by assigning to it
the obj.radius */
Circle(cosnt Circle &obj) { radius = obj.radius; }
private:
int radius;

};

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/b3945d81cdae8ba6?tvc=2

-N
 
R

Rolf Magnus

karthikbalaguru said:
Hi,

What is the difference between assignment operator and Copy
constructor.

I got the following info, But have some quries based on it.

Circle C1,C2;
C1 = C2; /* Assignment Operator */
Circle C3 = C2; /*Copy Constructor*/

In Above example, It states that for Copy Constructor, the compiler
creates a new object C3 and and copies the data from C2 member-by-
meber, into C3.
It also states that the Assignment operator does not create an object.

That's correct.
But How ? Actually 'Circle C1' has already been done before doing 'C1
= C2'.
Exactly.

So, how does it state the 'Assignment operator does not create an
object' ?

Uhm? Because actually, 'Circle C1' has already been done (created) before
doing 'C1 = C2'. So how could 'C1 = C2' create the object if it's already
created?
What actually is the difference between the assignment operator and
the copy constructor ?

You already posted it. I'm not sure what you don't understand about it.
 

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