Constructor Help (Deep Copy content)

Im not sure how to do this but I want wad2 to be created as a copy of wad1 so I can demonstrate a deep copy. Thanks guys! Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;
/*
 * 
 */
class Deep{
public:
    char *array;
    Deep();
    Deep(const Deep &copy);
    void setArray(int a,int b,int c,int d);
    void display();
};

int main() {
    cout << "Deep Copy 1" << endl;
    // creating deep copy 1
    Deep wad1;
    
    cout << "\nDeep Copy 2 created of Wrapper Deep" << endl;
    // creating copy of deep copy 1
    //////////////////////////////////problem area////////////////////////////////
    Deep wad2;
    
    // changing deep copy 1 while copy2 stays the same
    cout << "\nAfter changing the contents of 1, 1 and 2 =" << endl;
    wad1.setArray(123,124,125,126);
    
    wad1.display();
    wad2.display();
    
    return 0;
}

Deep::Deep(){
    array = new char[5];
    *array = 97;
    *(array+1) = 98;
    *(array+2) = 99;
    *(array+3) = 100;
    
    for (int i = 0; i<5; i++)
        cout << array[i] << " ";
}
Deep::Deep(const Deep &copy){
    char* deep = new char(*array);
    
    for (int i = 0; i<5; i++)
        cout << array[i] << " ";
}
void Deep::setArray(int a, int b, int c, int d){
    *array = a;
    *(array+1) = b;
    *(array+2) = c;
    *(array+3) = d;
}
void Deep::display(){
    for (int i = 0; i<5; i++)
        cout << array[i] << " ";
    
    cout << endl;
}
Last edited on
Your code has some undefined behavior. For example in the constructor

1
2
3
4
5
6
7
8
9
10
Deep::Deep(){
    array = new char[5];
    *array = 97;
    *(array+1) = 98;
    *(array+2) = 99;
    *(array+3) = 100;
    
    for (int i = 0; i<5; i++)
        cout << array[i] << " ";
}


you defined array of 5 elements (characters) but only 4 elements were filled.

Also instead of using ASCII codes it is better to use their symbolic representation.
I suggest to name magic number 5. And your class needs the copy assignment operator.

Here is updated class

1
2
3
4
5
6
7
8
9
10
11
class Deep{
public:
    Deep();
    Deep(const Deep &copy);
    Deep & operator =( const Deep &copy ); 
    void setArray(char a,char b,char c,char d);
    void display() const;
private:
    enum { SIZE = 5 };
    char *array;
};


So I would write the constructor the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Deep::Deep()
{
    array = new char[SIZE];

    *array = 'a';
    *(array+1) = 'b';
    *(array+2) = 'c';
    *(array+3) = 'd';
    *(array+4) = '\0';
}

Deep::Deep(const Deep &copy)
{
    array = new char[SIZE];
    
    for ( int i = 0; i < SIZE; i++ ) array[i] = copy.array[i];
}

Deep & Deep::operator =( const Deep &copy )
{
    if ( this != &copy )
    {
        for ( int i = 0; i < SIZE; i++ ) array[i] = copy.array[i];
    }

    return ( *this );
}

void Deep::setArray( char a, char b, char c, char d)
{
    *array = a;
    *(array+1) = b;
    *(array+2) = c;
    *(array+3) = d;
    *(array+4) = '\0';
}
void Deep::display() const
{
    for (int i = 0; i < SIZE - 1; i++ )  cout << array[i] << " ";
    cout << endl;
}

Last edited on
Solved! Thank you so much! That makes total sense now.
Topic archived. No new replies allowed.