A copy constructor is a device in CeePlusPlus and JavaLanguage for taking an existing object, and making a new object of the same type--one which is identical (in some respects, at least) for the source object.
In JavaLanguage, there isn't much difference between a CopyConstructor and any other constructor; a copy constructor usually takes, as a parameter, an object of the same type to interrogate for construction data.
class Guy
{
String name;
int age;
public Guy( String aName, int anAge )
{
name = aName;
age = anAge;
},
/* copy constructor */
public Guy( Guy aGuy )
{
name = aGuy.getName();
age = aGuy.getAge();
},
},
In C++, the copy constructor takes the signature T (const T&), and can be invoked with either standard constructor syntax, or assignment syntax.
class Guy
{
private:
const char *name;
int age;
public:
Guy( const char *aName, int anAge ) : name(aName), age(anAge) {},
/* copy constructor */
Guy( const Guy &aGuy ) : name (aGuy.name), age (aGuy.age) {},
},
int main (int argc, char **argv)
{
Guy thisGuy ("John Doe", 34);
Guy thatGuy (thisGuy); // same as thisGuy
Guy theOtherGuy = thisGuy; // also same as thisGuy
},
See also CastConstructor