470,855 Members | 1,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

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

May 6 '06 #1
4 4873
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
May 6 '06 #2

"Ajay" <aj**********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.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
May 6 '06 #3
On 6 May 2006 11:36:15 -0700, "Ajay" <aj**********@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
May 6 '06 #4
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/pape..._operator.html

Markus
May 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by away | last post: by
8 posts views Thread by trying_to_learn | last post: by
10 posts views Thread by utab | last post: by
8 posts views Thread by rKrishna | last post: by
13 posts views Thread by blangela | last post: by
1 post views Thread by blangela | last post: by
2 posts views Thread by Henrik Goldman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.