2 times Operator Overloading

Hey guys!

Sorry for my rather broken english beforehand, I hope no one minds.

My problem is following:

I have coded a class which allows me to resize my arrays which were allocated dynamicly (I'm not sure if I'm translating it right, but I mean something like this: int* temp = new int[size+b]


Everything works fine, but now I want to make them usable for common operations like

cout << a[3];

while a is the name of the object representing the array. The object contains 3 member variables: int size, int counter(to keep track how much space is left before a resize is needed) and int* list which points to the content of the array.

I made following:

1
2
3
4
int operator[](int a)
{
	return list[a];
}


so cout << a[3]is no problem anymore.

But this is where it gets tough.
I want to make it possible to make following statement:

a[3] = 5;

i want to be able to overwrite the existing entry at number 3 with the value 7.

For this, I need 2 operators.

operator[] and operator=

But I don't know how to combine these two.

1
2
3
4
5
6
7
8
9
10
int operator[](int a)
{
        overwrite = a;
	return list[a];
}

int operator=(int b)
{
		list[overwrite] = b;
}


But it won't work. I've included overwrite as a member variable, tried to put both into one operator overload. But it doesn't work.

How can i make the command

a[3] = 7;

valid?

Greetings,
Himzo
Last edited on
Just make operator[] return a reference.
return *list?
because return *list doesn't work :/

Here is the whole 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
90
91
92
#include <iostream>
using namespace std;

class DynArr
{
private:
    int overwrite;
	int size;
	int counter;
	int* list;

public:
	DynArr(int a=0)
	{
		size = a;
		counter=0;
		list = new int[a];
		overwrite=0;
	}

	void resize(int b=0)
	{
		if(b!=0)
		{
			int* temp = new int[size+b];
		
			for(int i=0;i<size;i++)
			{
				temp[i] = list[i];
			}

			delete[] list;
			list=temp;
			size += b;
		}
	}

	void print()
	{
		cout << endl;
		for(int i=0;i<size;i++)
		{
			cout << list[i] << endl;
		}
	}

	void input(int* c, int len)
	{
		if(counter+len>size)
		{
			resize(counter+len-size);
		}

		for(int i=0;counter<size;i++)
		{
			list[counter]=c[i];
			++counter;
		}
	}

	int operator[](int a)
	{
        overwrite = a;
		return *list;
	}

	int operator=(int b)
	{
		list[overwrite] = b;
		return *list;
	}
		
};

int main()
{
	DynArr a(5);
	int* b = new int[5];

	for(int i=0;i<5;i++)
	{
		cin >> b[i];
	}

	a.input(b, 5);
	a.print();
	cout << a[3];
	a[3]=7;
	cout << a[3];
	cin >> b[5];
	return 0;
}



This results in "88: non-lvalue in assignment"
Last edited on
In general it is better to define two overload operator-functions

1
2
3
4
int & operator[](int a)
{
	return list[a];
}

and

1
2
3
4
int operator[](int a) const
{
	return list[a];
}
Thank you! This has solved my problem!
Would you mind explaining to me why this is working? I would like to understand why my method isn't working, but your's is.

Greetings
Himzo
here you return a reference to an array element.

1
2
3
4
int & operator[](int a)
{
	return list[a];
} 


Consider the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
   int x = 10;
   int &r = x;  // r is reference of x;

   std::cout << "x = " << x << std::endl;
   std::cout << "r = " << r << std::endl;

   r = 20;

   std::cout << "x = " << x << std::endl;
   std::cout << "r = " << r << std::endl;

   return 0;
}
I assume x and r are now both 20?
You are right.
I understand.
Thank you for your support, I appreciate your and Peters helpfulness :)
Topic archived. No new replies allowed.