简体   繁体   中英

C++ Array Deep Copy Constructor

From "Programming: Principles and Practices using C++ - Bjarne Stroustrup" example on copying arrays:

18.3.1 Copy constructors

class vector {
    int sz; // the size
    double* elem; // a pointer to the elements
public:
    vector(int s) :
            sz { s }, elem { new double[s] } {
        for (int i = 0; i < sz; ++i)
            elem[i] = 0.0; // initialize
    } // constructor
    ~vector() {
        delete[] elem;
    } // destructor
    int size() const {
        return sz;
    } // the current size
    double get(int n) const {
        return elem[n];
    } // access: read
    void set(int n, double v) {
        elem[n] = v;
    } // access: write
    vector(const vector& arg)
// allocate elements, then initialize them by copying
    :
            sz { arg.sz }, elem { new double[arg.sz] } {
        copy(arg.elem, arg.elem + arg.sz, elem); // std::copy(); see §B.5.2
    }
};
int main(int argc, char ** argv) {
    vector v(3); // define a vector of 3 elements
    v.set(2, 2.2); // set v[2] to 2.2
    vector v2 = v;
    v.set(1, 99); // set v[1] to 99
    v2.set(0, 88); // set v2[0] to
    cout << v.get(0) << ' ' << v2.get(1);
    return 0;
}

Why are the actual array members copied and not just their addresses? elem is a pointer and is not de-referenced in the copy command.

Your confusion stems from the distinction between value types and reference types .

In standard C++ practices, everything is a value type unless specified otherwise. Copies of objects are fully distinct unless specifically make to be otherwise.

This is why altering a copy of a vector should not alter the original vector it was copied from regardless of how the internal workings of the vector function .

The fact that the vector is implemented using dynamically allocated memory internally is an implementation detail that has no bearing on the interface of the vector itself, which presents itself as a value type (by default)

To answer the question about why the array members are copied, and not just their addresses. It might be helpful to explain the difference between a shallow copy and a deep copy. Let's say we have two pointers to two different arrays, A and B.

A = [0, 1, 2, 3, 4] 
B = [9, 10, 11, 12, 13] 

Lets copy the contents of A to B. A shallow copy would involve just changing pointers. B = A

This is not always ideal, because any element you change in A is also changed in B, because they point to the same location in memory.

If you perform a deep copy, you copy all elements over from A into B.

A = [0, 1, 2, 3, 4] 
B = [0, 1, 2, 3, 4]

If you change an item in A, it will not affect the items in B. ie changing

A[0] = 10

then,

A = [10, 1, 2, 3, 4] 
B = [0, 1, 2, 3, 4]

I hope that helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM