Question

My code can also be found here.

This code would work (but there is much code duplication):

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;
}

Below is my attempt to avoid writing same thing three times... but it does not work. Isn't it possible to make that code shorter?

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);
}
Was it helpful?

Solution

You cannot call a constructor as a member function. You can create a member function, and call it from the constructor, and from all the other places.

void Employee::Init(const char* n, const char* e, double s)
{
        name = new char [strlen(n)+1];
        strcpy(name, n);
        strcpy(EGN, e);
        salary=s;
}

void Employee::Init(const Employee &x)
{
        Init(x.name, x.EGN, x.salary);
}

Employee::Employee(char* n, char* e, double s)
{
    Init(n,e,s);
}


Employee::Employee(const Employee& x)
{
     Init(x);
}

void Employee::operator=(const Employee& x)
{
        delete[] name;
        Init(x);
}

OTHER TIPS

What you are attempting is not allowed by the language. However, C++11 allows delegating constructors, so you could do something like this:

Employee::Employee(const Employee& x) : Employee(x.name, x.EGN, x.salary){}

Note that one constructor is called in the initialization list of the other.

Before C++11, an option would have been to have some kinf of initialization function called from all the constructors. However, this is semantically different because the function call performs assignment to member variables, not initialization.

Try this in C++11

Employee::Employee(char* n, char* e, double s)
{
        name = new char [strlen(n)+1];
        strcpy(name, n);
        strcpy(EGN, e);
        salary=s;
}

// Constructor chaining (delegation) like this.
Employee::Employee(const Employee& x)
    : Employee(x.name, x.EGN, x.salary)
{}


// Use copy and swap for the assignment operator:

// Common convention to return a reference to yourself to allow chaingin.
Employee& Employee::operator=(Employee x)   // Pass by value to get the copy.
{ 
    x.swap(*this);                          // Then swap members
    return *this;
}                                           // Destructor will then cleanup.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top