私のコードもここにあります。
このコードは機能します(ただし、コードの重複が多くあります)。
Employee::Employee(const Employee& x)
{
name=new char [strlen(x.name)+1];
strcpy(name, x. name);
strcpy(EGN, x.EGN);
salary=x.salary;
}
void Employee::operator=(const Employee& x)
{
delete[] name;
name=new char [strlen(x.name)+1];
strcpy(name, x. name);
strcpy(EGN, x.EGN);
salary=x.salary;
}
Employee::Employee(char* n, char* e, double s)
{
name = new char [strlen(n)+1];
strcpy(name, n);
strcpy(EGN, e);
salary=s;
}
以下は、同じことを3回書くことを避けるための私の試みです...しかし、それは機能しません。そのコードを短くすることはできませんか?
Employee::Employee(char* n, char* e, double s)
{
name = new char [strlen(n)+1];
strcpy(name, n);
strcpy(EGN, e);
salary=s;
}
Employee::Employee(const Employee& x)
{
Employee(x.name, x.EGN, x.salary);
}
void Employee::operator=(const Employee& x)
{
delete[] name;
Employee(x);
}