473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

'C1 = C2' == Copy Constructor

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

Sep 1 '07 #1
3 2310
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;
};

Sep 1 '07 #2
On Sep 1, 8:59 am, "deepakvs...@gmail.com" <deepakvs...@gmail.com>
wrote:
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....cdae8ba6?tvc=2

-N

Sep 1 '07 #3
karthikbalaguru wrote:
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.

Sep 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.