Benefits of copy constructor over assignment operator



 DEVELOP > c-Plus-Plus > Benefits of copy constructor over assignment operator

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Ajay"
Date: 06 May 2006 01:36:15 PM
Object: Benefits of copy constructor over assignment operator
Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay
.

User: "Mike Wahler"

Title: Re: Benefits of copy constructor over assignment operator 06 May 2006 01:59:56 PM
"Ajay" <ajay.kumarCS@gmail.com> wrote in message
news:1146940575.445487.263910@u72g2000cwu.googlegroups.com...

Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay

class MyType
{
/* etc */
};
void foo(MyType arg)
{
}
int main()
{
MyType mt;
foo(mt); /* requires that 'MyType' has a
copy constructor */
return 0;
}
Depending upon the design of the 'MyType' type,
the needed copy constructor could be a compiler-
synthesized one (created if no user-defined
constructors are defined), or could need to be
a user-defined one (e.g. 'deep copy' needed).
Also, standard library containers require their
element types to be copyable as well as assignable.
'Copyable' means they must either be built-in
types, or have a (either default or user-defined)
copy constructor.
In my example above, with nothing else inside
the class definition, the compiler-generated
copy constructor will meet the requirement.
-Mike
.

User: "Mark P"

Title: Re: Benefits of copy constructor over assignment operator 06 May 2006 01:45:01 PM
Ajay wrote:

Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay

If you want to store your objects in standard containers (vector, list,
map, etc.) they need to be Assignable which in turn requires that they
have a copy constructor.
If you write all your code entirely from scratch then I suppose you can
simulate a copy ctor by constructing an object and immediately assigning
to it, but this doesn't seem very sensible.
Mark
.

User: "Markus Becker"

Title: Re: Benefits of copy constructor over assignment operator 11 May 2006 11:30:21 PM
Hi,
Ajay schrieb:

Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay

Have a look at this:
http://icu.sourceforge.net/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
Markus
.

User: "Roland Pibinger"

Title: Re: Benefits of copy constructor over assignment operator 06 May 2006 02:01:16 PM
On 6 May 2006 11:36:15 -0700, "Ajay" <ajay.kumarCS@gmail.com> wrote:

Hi all,
Can anybody please tell me that what exactly is the need of
copy constructor giving a real life example where i can not do things
with assgnment operator but can do things i want with copy constructor
only.Thanks in advance. ajay

A copy constructor, like any other constructor, creates an object from
scratch. An assignment operator (at least conceptually) destroys the
current contents of an object and replaces it with a copy of the
assigned contents. To answer your question, you cannot construct an
object with an assignment operator.
Best wishes,
Roland Pibinger
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER