Copy the contents of a unique array of pointers in the copy constructor

My class contains a unique pointer to an array. When the copy constructor is called, I want the class to create its own unique array of pointers and just copy the contents of the old unique array of pointers. I keep getting errors regarding the conversion from the const value, and I'm not sure how to get around it.

My pointer is declared private:

std::unique_ptr<Manager[]> managers;

I planned to just skip the array and copy manually, so I created this copy constructor:

Restaurant::Restaurant(const Restaurant &_r)
{
    Manager *_managers = _r.managers;
    for (int i = 0; i < MAX_MANAGERS; i++)
    {
        managers.get()[i] = _managers[i];
    }
}

It gives a const conversion error on this line:

Manager *_managers = _r.managers;

I just want to make a deep copy. How can I do this to make this work?

+4
2

, , ,   _r.managers std::unique_ptr<Manager[]>, .

:

Restaurant::Restaurant(const Restaurant &_r)
{
    for (int i = 0; i < MAX_MANAGERS; i++)
    {
        managers.get()[i] = _r.managers.get()[i];
    }
}

( )

Manager *_managers = _r.managers.get();

, :

for (int i = 0; i < MAX_MANAGERS; i++) {
        managers.get()[i] = _managers[i];
}
+3

, , managers std::unique_ptr<Manager[]>. Manager*, .

, unique_ptr, :

Manager *_managers = _r.managers.get();
+1

Source: https://habr.com/ru/post/1524194/


All Articles