Unexpected results when trying to copy an array

Hi, this is an exercise for a c++ class.

I'm trying to create a shallow copy of an array.

According to the book, the last line printed out should be the same 2 strings because of a shallow copy.

However, the arrays are different.

I thought I had setup a proper shallow copy constructor by just doing this: a = b.

Could someone please take a look and let me know what I did wrong?

Thanks

<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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma warning(disable: 4996)
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include <stack>
#include <conio.h>

using namespace std;

class WrapArrayShallow {
private:
	char *pch;

public:
	WrapArrayShallow();
	void displayWrapArrayShallow();
	void setWrapArrayShallow(char a, char b, char c, char d, char e);
	WrapArrayShallow(WrapArrayShallow &was);
	~WrapArrayShallow();

};

//main
int main()
{

	WrapArrayShallow was1;
	WrapArrayShallow *was2 = new WrapArrayShallow(was1);

	cout << "Now doing the same thing with WrapArrayShallow:\n\n";

	cout << "WrapArrayShallow 1:\n";
	was1.displayWrapArrayShallow();

	cout << "WrapArrayShallow 2 created using the copy constructor on 1:\n";
	was2->displayWrapArrayShallow();

	was1.setWrapArrayShallow('f', 'g', 'h', 'i', 'j');
	cout << "After changing the contents of WrapArrayShallow 1, 1 and 3 = \n";
	was1.displayWrapArrayShallow();
	was2->displayWrapArrayShallow();


	cout << "\n\n";
	system("pause");
	return 0;
}

//Class definitions

//Use array notation to load array
WrapArrayShallow::WrapArrayShallow()
{
	cout << "calling default WrapArrayShallow constructor\n\n";
	pch = new char[5];
	pch[0] = 'a';
	pch[1] = 'b';
	pch[2] = 'c';
	pch[3] = 'd';
	pch[4] = 'e';

}

//copy constructor
WrapArrayShallow::WrapArrayShallow(WrapArrayShallow& was)
{
	cout << "**** calling copy WrapArrayShallow constructor\n\n";
	pch = was.pch;
}

//Use pointer arithmetic to print array
void WrapArrayShallow::displayWrapArrayShallow()
{
	for (int i = 0; i < 5; i++)
		cout << *(pch + i) << " ";
	cout << "\n\n";
}

//Use array notation to set array
void WrapArrayShallow::setWrapArrayShallow(char a, char b, char c, char d, char e)
{
	pch = new char[5];
	pch[0] = a;
	pch[1] = b;
	pch[2] = c;
	pch[3] = d;
	pch[4] = e;
}


</code>












Last edited on
Relevant output:
calling default WrapArrayShallow constructor

**** calling copy WrapArrayShallow constructor

Now doing the same thing with WrapArrayShallow:

WrapArrayShallow 1:
a b c d e 

WrapArrayShallow 2 created using the copy constructor on 1:
a b c d e 

After changing the contents of WrapArrayShallow 1, 1 and 3 = 
f g h i j 

a b c d e 


You may want to have a look at line 83. Think about what it does for a while, and think about what this means for was2 when setWrapArrayShallow is called on was1.

Side note, despite there being several news in your program, I don't see one delete. While you don't technically have a memory leak yet, you're awfully prone to getting one with that code. Just a warning.

-Albatross
Albatross,

Thanks for the help. I think I see the problem in line # 83. It's declaring a brand new array. So even though I call it like:

was1.setWrapArrayShallow('f', 'g', 'h', 'i', 'j');

That will create a new array.

So I removed that line(#83) and the two arrays are now the same.

However is that the correct way of doing it?

Thanks!
Topic archived. No new replies allowed.