Copy contents of a unique pointer array in the copy constructor

Lucas

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?

4pie0

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];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

Pointer assignments in a copy constructor

From Dev

Trying to copy contents of array

From Dev

copy constructor copy pointer to abstract class

From Dev

Copy array in constexpr constructor

From Dev

Object array copy constructor

From Dev

How to copy contents of an array to a variable

From Dev

C++14 can not call copy constructor or operator= of class inheriting from unique pointer

From Dev

How to copy entire array in constructor

From Dev

Copy Constructor for a String Array in Java

From Dev

How to copy entire array in constructor

From Dev

Shallow copy a string array in the constructor

From Dev

Checking to see if a char pointer is null - Copy Constructor

From Dev

Writing safe copy-constructor with smart pointer

From Dev

How to define copy constructor and deallocate pointer

From Dev

copy constructor for a class with pointer to a user defined type

From Dev

C++ copy constructor with object pointer as argument

From Dev

Writing safe copy-constructor with smart pointer

From Dev

How to define copy constructor and deallocate pointer

From Dev

Copying a QThread pointer using a memcpy in a copy constructor

From Dev

Making copy of any pointer member varible in copy constructor

From Dev

Making copy of any pointer member varible in copy constructor

From Dev

Copy semantics for C++ unique pointer

From Dev

Copy semantics for C++ unique pointer

From Dev

Copy Constructor to transfer ownership of a unique_ptr

From Dev

Copy Constructor

From Dev

Copy contents of unique_ptr to unknown derived class

From Dev

How to copy a char pointer into a char array?

From Dev

How to copy from character array to character pointer?

Related Related

  1. 1

    Copy contents of a unique pointer array in the copy constructor

  2. 2

    Pointer assignments in a copy constructor

  3. 3

    Trying to copy contents of array

  4. 4

    copy constructor copy pointer to abstract class

  5. 5

    Copy array in constexpr constructor

  6. 6

    Object array copy constructor

  7. 7

    How to copy contents of an array to a variable

  8. 8

    C++14 can not call copy constructor or operator= of class inheriting from unique pointer

  9. 9

    How to copy entire array in constructor

  10. 10

    Copy Constructor for a String Array in Java

  11. 11

    How to copy entire array in constructor

  12. 12

    Shallow copy a string array in the constructor

  13. 13

    Checking to see if a char pointer is null - Copy Constructor

  14. 14

    Writing safe copy-constructor with smart pointer

  15. 15

    How to define copy constructor and deallocate pointer

  16. 16

    copy constructor for a class with pointer to a user defined type

  17. 17

    C++ copy constructor with object pointer as argument

  18. 18

    Writing safe copy-constructor with smart pointer

  19. 19

    How to define copy constructor and deallocate pointer

  20. 20

    Copying a QThread pointer using a memcpy in a copy constructor

  21. 21

    Making copy of any pointer member varible in copy constructor

  22. 22

    Making copy of any pointer member varible in copy constructor

  23. 23

    Copy semantics for C++ unique pointer

  24. 24

    Copy semantics for C++ unique pointer

  25. 25

    Copy Constructor to transfer ownership of a unique_ptr

  26. 26

    Copy Constructor

  27. 27

    Copy contents of unique_ptr to unknown derived class

  28. 28

    How to copy a char pointer into a char array?

  29. 29

    How to copy from character array to character pointer?

HotTag

Archive