Deep copy and operator overloading

Hi,

In main I instantiate two Polynomial objects -- p1 and p2:

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
int main()
{

    const int SIZE = 3;

    Polynomial *p1 = new Polynomial(SIZE);
    Polynomial *p2 = new Polynomial(SIZE);
    
    //Read data into p1
    std::cout << "Initialize Polynomial Coefficients (1)" << std::endl;
    std::cin >> *p1;

    //Read data into p2
    std::cout << "Initialize Polynomial Coefficients (2)" << std::endl;
    std::cin >> *p2;

    //Perform basic operations on the pointers

    std::cout << std::endl;
    std::cout << "Adding pointers ";
    std::cout << *p1 + *p2 << std::endl;

    std::cout << "Subtracting pointers ";
    std::cout << *p1 - *p2 << std::endl;	

    std::cout << "Multiplying pointers ";
    std::cout << (*p1) * (*p2) << std::endl;

    //...
    return 0;
}


The implementation file for Polynomial is as follows:

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
Polynomial::~Polynomial()
{
	delete [] this->m_Ptr;
	this->m_Ptr = NULL;
}

Polynomial::Polynomial(int size)
{
        this->size = size;
        this->m_Ptr = new double[this->size];
}

Polynomial::Polynomial(const Polynomial &other)
{
        //Perform a deep copy
	this->size = other.size;
	//this->m_Ptr = new double[this->size];
}

const Polynomial Polynomial::operator +(const Polynomial &other) const
{
	const Polynomial * result = this;
	for (int i = 0; i < other.size; i++)
	{
		*(result->m_Ptr + i) = *(other.m_Ptr + i) + *(this->m_Ptr + i);
	}
	return *(result);
}

const Polynomial Polynomial::operator -(const Polynomial &other) const
{
	const Polynomial * result = this;
	for (int i = 0; i < other.size; i++)
	{
		*(result->m_Ptr + i) = *(other.m_Ptr + i) - *(this->m_Ptr + i);
	}
	return *(result);
}

const Polynomial Polynomial::operator *(const Polynomial &other) const
{
	const Polynomial * result = this;
	for (int i = 0; i < other.size; i++)
	{
		*(result->m_Ptr + i) = *(other.m_Ptr + i) * *(this->m_Ptr + i);
	}
	return *(result);
}

std::ostream& operator <<(std::ostream &outputStream, const Polynomial &other)
{
	int n = other.size;
	for (int i = 0; i < other.size; i++)
	{
		if (*(other.m_Ptr + i) == 0)
		{
			n--;
			continue;
		}

		if (n > 0)
		{
			outputStream << *(other.m_Ptr + i) << "x^" << n << " ";
		}
		else
		{
			outputStream << *(other.m_Ptr + i);
		}
		n--;
	}
	return outputStream;
}

std::istream& operator >>(std::istream &inputStream, const Polynomial &other)
{
	for (int i = 0; i < other.size; i++)
	{
		inputStream >> *(other.m_Ptr + i);
	}
	return inputStream;
}


What works:

Adding two pointers. The output is correctly produced.

The problems:

The problem in particular occurs in main when p1 and p2 are attempted to be multiplied. The code attempts to release the memory upon multiplication but I receive a run-time error.

The output for the difference of the two polynomial objects is incorrect. It is displaying addresses.

Last edited on
lampshader wrote:
6
7
    Polynomial *p1 = new Polynomial(SIZE);
    Polynomial *p2 = new Polynomial(SIZE);
There is no need for pointers or dynamic memory in this case, you can just create the objects normally:
6
7
    Polynomial p1 (SIZE);
    Polynomial p2 (SIZE);
Have you considered also using a std::vector<double> instead of mucking around with pointers and dynamic memory by hand?
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
lampshader wrote:
The code attempts to release the memory upon multiplication but I receive a run-time error.
I don't believe you. The only call to delete[] is in your destructor.

You should note that lines 22, 32, 42 copy the pointer, not the object they point to.

http://www.LB-Stuff.com/pointers
Last edited on
OK. I removed * from p1 and p2 in main and the memory is able to release on from multiplying.

Quick question: how would I use a deep copy if I am allocating in the constructor?

If I choose to move the allocation from the ctor to the copy ctor, my << overload goes a little haywire. I then applied indexing to the pointer but it still proves problematic.
Last edited on
If you don't use pointers, you don't have to worry about this craziness.
lampshader wrote:
Quick question: how would I use a deep copy if I am allocating in the constructor?
It doesn't matter where the allocation occurs. You need to implement your own copy constructor and copy assignment operator because you are using pointers.
lampshader wrote:
If I choose to move the allocation from the ctor to the copy ctor, my << overload goes a little haywire. I then applied indexing to the pointer but it still proves problematic.
I don't know what you're talking about?
Topic archived. No new replies allowed.