"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