Operator overloading

Would anyone mind explaining this? I get overloading the extraction, insertion, and binary operaotrs. Logically, one would think that after deleting list you wouldn't be able to copy values into another list. I'm just a little confused...are we working with three lists in the function? My book sucks at explaining this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
const assignmentOverload& assignmentOverload::operator =
(const assignmentOverload &otherList)
{
	if(this != &otherList)
	{
		delete [] list;
		maxSize = otherList.maxSize;
		length = otherList.length;
		list = new int[maxSize];
		for(int i = 0; i < length; i++)
			list[i] = otherList.list[i];
	}
	return *this;
}

Look at a simpler assignment:

1
2
3
4
5
6
7
int foo = 5;
int bar = 6;

foo = bar; // this line does the following

// 1)  discards 'foo's old value of 5
// 2)  replaces it with the new value of 6 


line 6 in your code is discarding the old list. We're copying data from a new list now, so that old list is crap. You need to get rid of it.

You then allocate a new list, large enough to hold all the necessary data on line 9

And copy the data from 'otherList' to our new list on lines 10,11
Right, classes have a built in assignment operator, they keep emphasizing avoiding self assignment. Does the built in assignment avoid this as well? Okay, so the old list is destroyed, and otherList has the base address of the list, but if list is destroyed before it is copied into otherList, how does otherList know what values need to be stored into it? This is just supposed to make a member wise copy of the original list list, correct? And the return value returns the base address of list as well? Thank you for your help Disch.

Oh!!...I think I see what's going on....otherList IS list..right?
Last edited on
Here is the code segment I've been playing around with if it helps.

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
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class assignmentOverload
{
public:
	const assignmentOverload& operator=(const assignmentOverload& otherList);
	void print() const;
	void insertEnd(int);
	void destroyList();
	assignmentOverload(int size = 0);
private:
	int maxSize;
	int length;
	int *list;
};

int main()
{
	int xxx;
	assignmentOverload list1(10), list2, list3;
	int i, number;
	cout<<"Enter 5 integers: "<<endl;
	for(i = 0; i < 5; i++)
	{
		cin>>number;
		list1.insertEnd(number);
	}
	cout<<"\nlist1: "<<endl;
	list1.print();
	list3 = list2 = list1;
	cout<<"\nlist2: "<<endl;
	list2.print();
	list2.destroyList();
	cout<<"\nlist2: "<<endl;
	list2.print();
	cout<<"\nAfter destroying list2, list1: "<<endl;
	list1.print();
	cout<<"After destroying list2, list3: "<<endl;
	list3.print();
	cin>>xxx; // read output
	return 0;
}

void assignmentOverload::print() const
{
	if(length == 0)
		cout<<"The list is empty."<<endl;
	else
	{
		for(int i = 0; i < length; i++)
			cout<<list[i]<<" ";
		cout<<endl;
	}
}

void assignmentOverload::insertEnd(int item)
{
	if(length == maxSize)
		cout<<"The list is full."<<endl;
	else
		list[length++] = item;
}

void assignmentOverload::destroyList()
{
	delete [] list;
	list = NULL;
	length = 0;
	maxSize = 0;
}

assignmentOverload::assignmentOverload(int size)
{
	maxSize = size;
	length = 0;
	if(maxSize == 0)
		list = NULL;
	else
		list = new int[maxSize];
}

const assignmentOverload& assignmentOverload::operator =(const assignmentOverload &otherList)
{
	if(this != &otherList)
	{
		delete [] list;
		maxSize = otherList.maxSize;
		length = otherList.length;
		list = new int[maxSize];
		for(int i = 0; i < length; i++)
			list[i] = otherList.list[i];
	}
	return *this;
}
The default assignment operator does a member-wise copy such that self-assignment
just works without the need for self-assignment checking.

And yours would work too, without the need for self-assignment checking, if it were mindful
of exceptions. See:

http://www.cplusplus.com/articles/jsmith1/

jsmith wrote:
And yours would work too, without the need for self-assignment checking,


I don't think it would, because the first thing it does is delete the buffer. So if you self assign, you lose all the data immediately and then explode.

yoked88 wrote:
Okay, so the old list is destroyed, and otherList has the base address of the list, but if list is destroyed before it is copied into otherList, how does otherList know what values need to be stored into it?


There are 2 'list's here.

One belongs to 'this' and is being replaced.
The other belongs to 'otherList'

You are copying otherList's list to this's list.
You are not changing otherList's list, you are only reading it to know what to copy to your new list.

So you delete this's list, then recreate it to match the size of otherList's list. Then you copy the contents of otherLists's list over to this's list.
Clarified, thanks for the help guys, I got it!
Topic archived. No new replies allowed.