Define copy constructor? What is the use of copy constructor?

sunina's Avatar
Banned
# Define copy constructor? What is the use of copy constructor?

Last edited by shabbir; 15Feb2007 at 12:24.. Reason: Confine links to signatures only.
anantwakode's Avatar, Join Date: Apr 2006
Light Poster
Hi,

look in to this for more information on copy Constructor !

See,
if you have a class , having pointer variables pointing to another class
in that case if you want to new create object using existing one, and you want to use properties of existing object in to new except pointers (Otherwise both the object will point to same memory location) in that case you can use copy constructor for creating new instance from old one !

eg.
Code:
class SomeClass
{
AnotherClass *Ac;
public
SomeClass(Const SomeClass &SC )
{
//Other Properties = SC.OthereProperties
Ac=new AnotherClass();
}
};

Last edited by shabbir; 23Feb2007 at 14:45.. Reason: Code formatting.
anantwakode's Avatar, Join Date: Apr 2006
Light Poster
Hi,

look in to this for more information on copy Constructor !
http://en.wikipedia.org/wiki/Copy_constructor

See,
if you have a class , having pointer variables pointing to another class
in that case if you want to new create object using existing one, and you want to use properties of existing object in to new except pointers (Otherwise both the object will point to same memory location) in that case you can use copy constructor for creating new instance from old one !

eg.
Code:
class SomeClass
{
AnotherClass *Ac;
public
SomeClass(Const SomeClass &SC )
{
//Other Properties = SC.OthereProperties
Ac=new AnotherClass();
}
};
-Anant