Skip this one.
As you can say on behalf of, this is a constructor. Since the parameter accepts a reference to an object of the same type, this is a copy constructor (C ++ nomenclature).
( ), , . .
:
class Car {
char *LicencePlate;
public:
Car(char* plate, int size)
{
LicencePlate = new char[size];
strcpy(LicencePlate, plate);
}
~Car()
{
delete[] LicencePlate;
}
};
, . . LicencePlate. , . , :
Car a("abc",3);
:
Car b(a);
, . , , a.LicencePlate == b.LicencePlate. - ?
a , a.LicencePlate . undefined, b , b destructor (, , ).
, :
class Car {
char *LicencePlate;
int sz;
public:
Car(char* plate, int size)
{
LicencePlate = new char[size+1]();
strcpy(LicencePlate, plate);
sz = size;
}
Car(const Car& other)
{
LicencePlate = new char[other.sz+1]();
sz = other.sz;
strcpy(LicencePlate, other.LicencePlate);
}
~Car()
{
delete[] LicencePlate;
}
};
, ( ). , , :
Car a("abc",3);
Car b;
b = a;
. b , , .
, , .