-3

I have class Employee

class Employee
{
   public:
   char* Name;
   char* Position;
   int Age;

   Employee (const char *_name, const char *_position, int _age)
   {
      Name = new char[strlen (_name) + 1];  
      strcpy(Name, _name);
      Position = new char[strlen (_position) + 1];
      strcpy(Position, _position);
      Age = _age;
   };
   ~Employee ()
   {
      delete [] Name;
      delete [] Position;   
   };

}

I want to change value of field Position by pointer Employee* p.

 int main(void){
    Employee first("Josh", "secretary", 33);
    Employee *p; 
    p = &(first);
    p->Position = "salesman";
 }

Is that posible to change this value in this way? I know it that it could be done by changing char* to string. I suppose that I should use class operator '->' or '=' a then resize char* filed and use strcpy to change value but I have no idea how to start.

Hartag96
  • 33
  • 5
  • 2
    https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr – user0042 Dec 23 '17 at 12:19
  • 1
    https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new –  Dec 23 '17 at 12:20
  • 1) Is it possible? Yes, did you try? It would be exactly the same as doing `first.Position = "salesman";` 2) Such code would introduce a memory leak, since original `Position` is never `delete`d. – Algirdas Preidžius Dec 23 '17 at 12:20
  • 2
    Names that begin with an underscore followed by a capital letter (`_Name`, `_Position`) are reserved for use by the implementation. Don't use them in your code. – Pete Becker Dec 23 '17 at 12:25
  • After your edit: such code would exhibit undefined behavior upon the execution of destructor, due to trying to `delete` what was not `new`ed (memory where `"salesman"` is stored) **in addition** to having memory leak for the same reason as before. – Algirdas Preidžius Dec 23 '17 at 12:26
  • 1
    *"I know it that it could be done by changing char** to string."* - Do you also know *why* it works with `std::string`? – Christian Hackl Dec 23 '17 at 12:40
  • string object allow assign a new value to the string, replacing its current contents by string::operator= – Hartag96 Dec 23 '17 at 12:52

1 Answers1

0

You can do it, but you're causing a memory leak since you never delete the old Position. Also, since your destructor does delete[] Position;, that will cause undefined behavior since the value of Position is no longer a dynamically-allocated pointer.

You should provide a member function to set the position, then it can free the previous position first, and copy the new string into dynamic memory created with new.

void Employee::setPosition(const char *new_position) {
    delete[] Position;
    Position = new char[strlen (new_position) + 1];
    strcpy(Position, new_position);
}

If you don't want a new member function, you have to do it in the caller:

delete[] p->Position;
p->Position = new char[sizeof "salesman" + 1];
strcpy(p->Position, "salesman");

This would be a bad design, though. The purpose of using classes is to hide all these implementation details. Ideally, properties like Position should be a private member, not public, and everything should be done using member functions.

Barmar
  • 596,455
  • 48
  • 393
  • 495