myclass m and operator * ( *m = value )

Hello everyone!

I created my vector class:
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once
#ifndef SPARKLYVECTOR_H
#define SPARKLYVECTOR_H
template <class T>
class spvec
{
public:
	T *data;
	int size;
	int reserved;
	int reservstep;

	spvec()
	{
		size = 0;
		reserved = 0;
		reservstep = 1;
		data = 0;
	}

	spvec(int reservstep)
	{
		size = 0;
		reserved = 0;
		reservstep = reservstep;
		data = 0;
	}

	spvec(T *indata, int insize)
	{
		size = insize;
		reserved = 0;
		reservstep = 1;
		data = indata;
	}

	spvec(T *indata, int insize, int reservstep)
	{
		size = insize;
		reserved = 0;
		reservstep = reservstep;
		data = indata;
	}

	~spvec()
	{
		if( size || reserved ) delete [] data;
	}

	void destroy()
	{
		size = 0;
		reserved = 0;
		delete [] data;
	}

	void makeroom(int howmuch)
	{
		T *p = new T[size+reserved+howmuch+reservstep];
		for( int a = 0; a < size; a++ ) p[a] = data[a];
		if(size) delete [] data;
		data = p;
		reserved += howmuch + reservstep;
	}

	void takeroom( int howmuch )
	{
		int needs = reserved - howmuch;
		if( needs < 0 ) makeroom(needs / -1);
		size += howmuch;
		reserved -= howmuch;
	}

	void trim()
	{
		T *p = new T[size+reservstep];
		for( int a = 0; a < size; a++ ) p[a] = data[a];
		if(size) delete [] data;
		data = p;
		reserved = reservstep;
	}

	void remove(int pos)
	{
		reserved++;
		for( int a = pos; a < size - 1; a++ ) data[a] = data[a+1];
		size--;
	}

	void remove(int pos, int howmuch)
	{
		reserved += howmuch;
		for( int a = pos; a < size - 1; a++ ) data[a] = data[a+howmuch];
		size -= howmuch;
	}

	void reverse()
	{
		T temp;
		int v = 0;
		for( int a = 0; a < size / 2; a++ ) 
		{
			v = size - (a + 1);
			temp = data[a];
			data[a] = data[v];
			data[v] = temp;
		}
	}

	void resize(int newsize)
	{
		T * t = new T[newsize + reservstep];
		int b = size;
		if (newsize < size) b = newsize;

		if (b) for (int a = 0; a < b; a++) t[a] = data[a];
		if (size) delete[] data;
		size = b;
		data = t;
	}

	void clean()
	{
		remove(0, size);
		trim();
	}

	void move( int what, int to )
	{
		int a; T temp;
		if( what > to )
		{
			temp = data[what];
			for( a = what; a > to; a-- ) data[a] = data[a-1];
			data[to] = temp;
			return;
		}

		temp = data[what];
		for( a = what; a < to; a++ )
		{
			data[a] = data[a+1];
		}
		data[to] = temp;
	}

	void move( int what, int howmuch, int to )
	{
		howmuch--;
		if( what > to )
		{
			for( int a = 0; a <= howmuch; a++ ) move(what + howmuch, to);
			return;
		}
		for( int a = 0; a <= howmuch; a++ ) move(what, to + howmuch);
	}

	void push( T *in, int insize )
	{
		int i = size;
		takeroom( insize );
		for( int a = 0; a < insize; a++ ) data[i+a] = in[a];
	}

	void push( int pos, T *in, int insize )
	{
		int i = size;
		takeroom( insize );
		int a;
		for( a = size - 2; a >= pos; a-- ) data[b + insize] = data[b];
	}

	void push( T in )
	{
		takeroom( 1 );
		data[size - 1] = in;
	}

	void push( int pos, T in )
	{
		takeroom(1);
		for( int a = size - 2; a >= pos; a-- ) data[a + 1] = data[a];
		data[pos] = in;
	}

	void swap(int what, int with)
	{
		T t;
		t = data[what];
		data[what] = data[with];
		data[with] = t;
	}

	void swap(int start, int end, int to)
	{
		for (int a = 0; a < end - start; a++) swap(start + a, to + a);
	}

	const spvec &operator+=(const T in)
	{
		this->push(in);
		return (*this);
	}

	const spvec &operator%=(const int in)
	{
		this->push(0, in);
		return (*this);
	}

	const spvec &operator=(const int in)
	{
		this->resize(in);
		return (*this);
	}

	const T operator[](const int in)
	{
		return this->data[in];
	}

	const spvec <T> *operator*[](const int in) // how? :D
	{
		return this->data[in];
	}

	spvec(spvec <T> &in)
	{
		cout << "here1" << endl;
		if (this == &in)
		{
			cout << "this == &in" << endl;
			return;
		}
		this->size = in.size;
		this->reserved = in.reserved;
		this->reservstep = in.reservstep;
		if (in.size || in.reserved)
		{
			this->data = new T[in.size + in.reserved];
			for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
		}
	}

	const spvec <T> &operator=(const spvec <T> &in)
	{
		cout << "here2" << endl;
		if (this == &in)
		{
			cout << "this == &in" << endl;
			return (*this);
		}

		if (this->size)
		{
			cout << "clear old" << endl;
			delete[] this->data;
		}
		this->size = in.size;
		this->reserved = in.reserved;
		this->reservstep = in.reservstep;
		if (in.size || in.reserved)
		{
			this->data = new T[in.size + in.reserved];
			for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
		}
		return (*this);
	}
};


And the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void testfunc(spvec<int> *in) {
cout << "in add:" << (long long int)in->data << endl;
cout << "first in:" << *in[0] << endl; // <--- problem ( should print first in:1 )
}

int main() {
spvec<int> v
v += 1;
v += 2;
v += 3;
v += 4;
v += 5;
cout << "first here:" << in[0] << endl; // should print: first here:1
testfunc(&v);
}


How to solve that problem?
I dont know how to make that *something operator work for my class.

If i would create my class like this:
myclass *a = new myclass();
then i can access to variable functions liket his:
a->something
but when i have created [] operators then what now?
How would i use them?
Thanks!
Changed your operator* to:
1
2
3
4
	const spvec <T> *operator*(const int in)
	{
		return &this->data[in];
	}


Changed line in operator= (there was no b)
 
			for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];


Added to spvec class: (didn't really need friend, your whole class is public)
1
2
3
4
5
  friend std::ostream &operator<<(std::ostream &os,const spvec<T> &st)
  {
    os << st.data;
    return os;
  }


Changed testfunc:
1
2
3
4
void testfunc(spvec<int> *in) {
cout << "in add:" << (long long int)in->data << endl;
cout << "first in:" << (*in)[0] << endl; // <--- problem ( should print first in:1 )
}


changed like in main():
 
cout << "first here:" << v[0] << endl;  // should print: first here:1 


Compiles and does what you want
Last edited on
Oh, sorry... also ran this:
1
2
3
4
5
6
7
8
9
10
11
12
void testnew()
{
  spvec<int> *i = new spvec<int>;
  *i += 1;
  *i += 2;
  *i += 3;
  *i += 4;
  *i += 5;
  for(int idx = 0; idx < 5; ++idx)
    cout << idx << ": " << (*i)[idx] << endl;
  delete i;
}



0: 1
1: 2
2: 3
3: 4
4: 5


std::vector has at() perhaps you could put something like that in to make the syntax less awkward?
Last edited on
Just for giggles, added to spvec class:
1
2
3
4
5
6
7
	T at(const unsigned int idx)
	{
	  std::exception e;
	  if(idx > size - 1) throw std::out_of_range("Index out of range");
	  else
            return this->data[idx];
	}


changed testnew() to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void testnew()
{
  spvec<int> *i = new spvec<int>;
  *i += 1;
  *i += 2;
  *i += 3;
  *i += 4;
  *i += 5;
  try
  {
  for(int idx = 0; idx < 6; ++idx) // iteration should throw out_of_range at idx = 6
    cout << idx << ": " << i->at(idx) << endl;
  } catch(const std::exception &e) {
    cout << "Exception occurred: " << e.what() << endl;
  }
  delete i;
}


Output:

0: 1
1: 2
2: 3
3: 4
4: 5
Exception occurred: Index out of range
Last edited on
it works even without:
1
2
3
4
5
  friend std::ostream &operator<<(std::ostream &os,const spvec<T> &st)
  {
    os << st.data;
    return os;
  }

perhaps my compiler add it automatically.
Btw, thank you for the reply ^^
Another problem.
1
2
3
4
5
6
7
8
9
10
11
int main() {
spvec<int> v
v += 1;
v += 2;
v += 3;
v += 4;
v += 5;
v[4] = 10; // Error	1	error C2106: '=' : left operand must be l-value
// but this works:
int u = v[3];
}


Is there any tutorial out there where i can see all those possibilities?
Every time i do the research i find tutorial about basic class making or just an question about some people asking help like i do.

I'm not quite sure how to call those things or search.
To be honest I'm not even sure what am i doing ... all i know for now that i have to recreate operators for my class for them to work as they should.
I also discover that i can have any operator act like i want, what is really fun.

one example: v %= 1; // adds int:1 to first place, v += 1 adds int:1 to last place
Problem was in your operator[]. You had:
1
2
3
4
const T operator[](const int in)
	{
		return this->data[in];
	}


This was returning a read-only value of T taken from data[in]

Changed to:
1
2
3
4
    T &operator[](const int in)
    {
        return this->data[in];
    }


This returns a writable reference to data[in] so you can make the assignment.
'm not quite sure how to call those things or search.
To be honest I'm not even sure what am i doing ... all i know for now that i have to recreate operators for my class for them to work as they should.
I also discover that i can have any operator act like i want, what is really fun.

one example: v %= 1; // adds int:1 to first place, v += 1 adds int:1 to last place


The operators work automatically for all standard data types, but remember if you are creating a new data type (in your case a vector implementation) you are going to have to define how those operators interact with your class. This gives you freedom to interpret how you believe the operators most naturally interact with your class.
Topic archived. No new replies allowed.