0

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?

  • 2
    Why won't you try to compile it, and make compiler tell you if it is legal or not? You even have wrote the code, lol. It's probably more work to write a question than try build the code. Also probably a good advice would be to encourage use of `std::string` if you are dealing with strings in `c++` and have the option. They are way more friendly to use than ansi c style strings IMO. – luk32 Jan 07 '14 at 12:45
  • It is ok ,since you are not try not update the content of the point – michaeltang Jan 07 '14 at 12:48
  • @ luk32 Well, you are right, I should've! but I'm having trouble with the compilers I'm using right now; and I didn't want to waste time meanwhile! – user2750466 Jan 07 '14 at 12:49
  • @user2750466 Well, but will have to compile it eventually. There is no waste of time. You need to do the work anyways, unless it's some kind of theoretical problem ... – luk32 Jan 07 '14 at 12:54
  • @ luk32 , I'm kinda new to C++, can you tell me please how and where exactly in my code I can use- std::string – user2750466 Jan 07 '14 at 12:55
  • 1
    @user2750466 Everywhere in the code where you use `char*` for storing a string. I cannot tell you exactly how, because I would need to rewrite almost whole of your code, which you cannot compile yet anyways. And I feel it would be a good experience to do it yourself. – luk32 Jan 07 '14 at 12:59

1 Answers1

1

suppose employeeName = "hello" using const char* employeeName as parameters means you can not update the content of the point, I indicates that you can't change hello to hallo. It is ok with your code

michaeltang
  • 2,850
  • 15
  • 18
  • The thing is that I'm sending a char* to const char*, I thought this might be problematic. – user2750466 Jan 07 '14 at 13:11
  • char** can't be converted to const char ** try this definittion Employee(const char* employeeName, int salary, char* const* workingHours) – michaeltang Jan 07 '14 at 13:29