I wrote the the next code in the main:
int main{
Employee *employee1 = NULL;
char *empName1=NULL;
char *workHours[7];
for (int ii=0;ii<7;ii++)
{
workHours[ii] = new char[5];
}
if (empName1 != NULL) {delete empName1;}
empName1 = new char[y_str-x_str];
// I read "workHours[ii]" from the stdin using strncpy
// here There's a block of code that is irrelevant to my question...
//......
//..
employee1 = new Employee(empName1,y_int,workHours);
}
Now, Employee is the constructor in a class called "Employee", here is the class:
class Employee {
public:
Employee(const char* employeeName, int salary, const char** workingHours);
char* getName();
int getSalary();
int calcWeeklySalary();
virtual ~Employee();
private:
char name[MAX_LINE_SIZE];
int empSalary_;
char* workHours[7];
};
And the implement of the constructor:
Employee:: Employee(const char* employeeName, int salary, const char** workingHours)
{
int i=0;
strcpy(name,employeeName);
empSalary_=salary;
for(i=0; i<7; i++)
{
strcpy( workHours[i] ,workingHours[i]);
}
}
I want to copy a string from "employee" to "name" (a private variable in the class), also from "workingHours[i]" to "workHours[i]" (class's private), using strcpy as you can see, and strcpy 2nd parameter has to be const char* as known.
So, my question is: is that legal what I did? I mean- "employee" is char* and "workingHours" is char**, while in Employee's signature I wrote "const".
And in case it's not- is there any other way to copy the strings from the Employee's parameters (sent by main) to the class's private variables?