5

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

My pointer is declared under private like this:

std::unique_ptr<Manager[]> managers;

I planned to just loop through the array and copy manually, so I made 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 the const convert error on this line:

Manager *_managers = _r.managers;

I just want to make a deep copy. How can I go about it to make this work?

5
  • 1
    Isn't _r.managers a unique_ptr? You can't assign it to something else. Jan 29, 2014 at 0:23
  • 1
    Also, why the unique_ptr if you want copy semantics? Wouldn't an std::vector<Manager> be better suited? Jan 29, 2014 at 0:30
  • he want just each class instance to have a unique_ptr to some array. Does usage of unique_ptr in a class imply a copy prohibition? If the object pointed to is indeed unique, then he don't want to make it shared_ptr, but still he want to copy class instances.
    – 4pie0
    Jan 29, 2014 at 0:37
  • @piotruś: With unique_ptr you have to do the copy manually, which means writing a copy constructor and copy assignment. vector has one built-in, so you don't have to write either of those. Jan 29, 2014 at 0:38
  • of course appropriate copy constructor and assignment operators are needed in such case, this is what he does
    – 4pie0
    Jan 29, 2014 at 0:41

3 Answers 3

4

The reason that it won't compile is that _r.managers is of type std::unique_ptr<Manager[]>, but you want to initialize a raw pointer with this.

just change it to:

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

or first take a smart pointer's data (which is an array)

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

and then you can use it as was before:

for (int i = 0; i < MAX_MANAGERS; i++) {
        managers.get()[i] = _managers[i];
}
3
  • This got it to compile, but the application crashes at the assignment. I printed out the values to verify that _r is there, but they crash upon assignment attempt.
    – Lucas
    Jan 29, 2014 at 0:48
  • probably your array is not initialized. check this, and write also assignment operator for your class Restaurant. This is necessary. At the moment it might be just: Restaurant& operator=(const Restaurant &_r) { if ( &_r != this) { for (int i = 0; i < MAX_MANAGERS; i++) { managers.get()[i] = _r.managers.get()[i]; } } return *this; }
    – 4pie0
    Jan 29, 2014 at 0:50
  • Yes, I forgot to initialize the array and I forgot to initialize the unique pointer in the class being copied to. I have it working now. Thanks.
    – Lucas
    Jan 29, 2014 at 0:58
1

In the line giving you an error, managers is an std::unique_ptr<Manager[]>. You're trying to assign it to a Manager*, which won't work.

You can fix it by taking the raw pointer of of the unique_ptr, for example:

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

In order to copy the content of unique_ptr<>, you might want to use "deep copy", this means that you write copy constructor in class Manager and a clone function. Example for copy constructor:

Manager(Manager const& manager) {
  name = manager.name;
  title = manager.title;
}

clone function:

unique_ptr<Manager> clone() const {
  return make_unique<Manager>(*this);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.