Deep copy constructor array class C++

Dandamann

Take the class given below as an example, it is basically just an array within a class.

class MyArray{
    public:
        MyArray(int maxCapacity = 1024){
            ptr = new int[maxCapacity];
            numOfElements = 0;
        }
        ~MyArray();
    private:
        int numOfElements; // current number of elements in array
        int *ptr; //pointer to the array data
};

What is the best way to implement a copy constructor for such a class.

Me and a friend are having an argument about it.

My belief is that it would require you to have another capacity variable that stores the maxCapacity argument of the constructor, however when given this question in an exam this variable was not included.

My friend believes that performing a copy that creates the new array based off the value of the numOfElements variable (in some arbitrary way) would suffice.

My issue with his suggestion is that this is not strictly a 'deep copy' as I would think a deep copy would imply that the new object is identical in every way except memory location.

Thanks, Dan

Paweł Stawarz

What is the best way to implement a copy constructor for such a class.

Well - the algorithm is quite simple. When copying A into B (so doing MyArray A; MyArray B(A)), the way here is:

  1. Allocate the needed space to store A.numOfElements elements,
  2. Set B.numOfElements to A.numOfElements,
  3. Copy numOfElements elements from A.ptr to B.ptr (possibly calling memcpy or - preferably - std::copy).

And taking into account your second concern:

My belief is that it would require you to have another capacity variable that stores the maxCapacity argument of the constructor (...) My friend believes that performing a copy that creates the new array based off the value of the numOfElements variable (in some arbitrary way) would suffice.

Quoting Wikipedia (text emphasized by me):

An object copy is an action in computing where a data object has its attributes copied to another object of the same data type.

So the answer is: yes, it would suffice, since maxCapacity isn't an attribute of MyArray. A deep copy is made when you actually copy the contents of one object into another (not just point to existing ones like when doing a shallow copy). In this case, since A actually stores numOfElements elements, we can assume that - even if it can contain more - the rest is just rubbish, so we don't need to copy it.

On the other hand - the class is probably improperly designed. When accessing elements of ptr, there is no way to check if we're accessing the memory we can, or trying to set memory that doesn't belong to us. Since the class itself doesn't store information about the array size, there is no way it can warn us about overflowing (well it can "crash", but that's probably not the best way to design your classes). And although it's a peculiar design, it doesn't interfere with actually copying the contents of MyArray.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ copy constructor for a class with an array attribute

From Dev

Deep Copy Constructor in Java

From Dev

Deep Copy Constructor in Java

From Dev

Replace default copy constructor with copy constructor for base class (c++)

From Dev

why c style array in class without user-defined operator= support deep copy

From Dev

why c style array in class without user-defined operator= support deep copy

From Dev

C++ template class with argument copy constructor

From Dev

C++ template copy constructor on template class

From Dev

c++ - copy constructor for template class

From Dev

C++ Array of Objects with a copy constructor

From Dev

Initialize an array member of class when class's copy constructor is deleted

From Dev

how to make a deep copy of a swift array of class objects

From Dev

Why not implement deep copy in default copy constructor?

From Dev

Why not implement deep copy in default copy constructor?

From Dev

shallow or deep copy or the array

From Dev

C++ Deep-copy of a class with assignment operator

From Dev

Copy constructor of template class

From Dev

Add copy constructor to a class

From Dev

Should one always define a copy constructor for deep copying pointers in a class with raw pointer members?

From Dev

Should one always define a copy constructor for deep copying pointers in a class with raw pointer members?

From Dev

Copy constructor not taking in the array size from a class object

From Dev

Copy array in constexpr constructor

From Dev

Object array copy constructor

From Dev

Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

From Dev

Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

From Dev

C++ Class Constructor With Array Member Variable

From Dev

Deep copy constructor with std::vector of smart pointers

From Dev

How to allocate memory with Constructor in deep copy?

From Dev

C++ behavior of a default(implicit) copy constructor in a derived class

Related Related

  1. 1

    C++ copy constructor for a class with an array attribute

  2. 2

    Deep Copy Constructor in Java

  3. 3

    Deep Copy Constructor in Java

  4. 4

    Replace default copy constructor with copy constructor for base class (c++)

  5. 5

    why c style array in class without user-defined operator= support deep copy

  6. 6

    why c style array in class without user-defined operator= support deep copy

  7. 7

    C++ template class with argument copy constructor

  8. 8

    C++ template copy constructor on template class

  9. 9

    c++ - copy constructor for template class

  10. 10

    C++ Array of Objects with a copy constructor

  11. 11

    Initialize an array member of class when class's copy constructor is deleted

  12. 12

    how to make a deep copy of a swift array of class objects

  13. 13

    Why not implement deep copy in default copy constructor?

  14. 14

    Why not implement deep copy in default copy constructor?

  15. 15

    shallow or deep copy or the array

  16. 16

    C++ Deep-copy of a class with assignment operator

  17. 17

    Copy constructor of template class

  18. 18

    Add copy constructor to a class

  19. 19

    Should one always define a copy constructor for deep copying pointers in a class with raw pointer members?

  20. 20

    Should one always define a copy constructor for deep copying pointers in a class with raw pointer members?

  21. 21

    Copy constructor not taking in the array size from a class object

  22. 22

    Copy array in constexpr constructor

  23. 23

    Object array copy constructor

  24. 24

    Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

  25. 25

    Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

  26. 26

    C++ Class Constructor With Array Member Variable

  27. 27

    Deep copy constructor with std::vector of smart pointers

  28. 28

    How to allocate memory with Constructor in deep copy?

  29. 29

    C++ behavior of a default(implicit) copy constructor in a derived class

HotTag

Archive