Class Templates

Ok so i had this code which gave me following output
Elements in List: 2 3 4 5 6 7 8 9 10 11 12
Elements in yourList: -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8


and then i changed the code using the class template so that i could process any datatype and not just int. but now that i changed it it gives me diff output. please tell me what i did wrong


Elements in myList: 4 5 6 7 8 9 10 11 12 0 1098850000
Elements in yourList: 11 12 0 1098850000 134255639 -5 -4 -3 -2 -1 0 1 2 3


here is the 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
#include <iostream>
using namespace std;

template <class T>
class myArray
{
public:
    myArray(int place, int _size)
    {
        X = place;
        end = _size - 1;
        array = new T[_size - place];
        for(int i = 0; i < _size - place; i++)
            array[i] = (T)X+i;
    }
  
    T& operator [](int index)
    {
        if(index <= end)
        {
            return array[index];
        }
        return *array;
    }
};
Last edited on
1
2
3
for (i = 2; i < 13; i++)
...
for (i = -5; i < 9; i++)
The array indices don't start at 2 and -5.
for the code i had before i added class templates. i had the exact same for loop and it worked perfectly fine.
@sun1520

Your constructor still doesn't look right, if the calls in main are correct. The two parameters should be the min index and end (as in max index plus one.) Otherwise the loops wouldn't work.

And once you've fixed your constructor, you can move onto operator []

(And please post additional methods with your corrections, otherwise the thread will be rather confusing for people reading it in the future.)

@naraku9333

If you look at the constructor calls, you can see this array is supposed to work with an offset index.

Andy
Last edited on
@Andy

Ohk This was wrong
1
2
3
4
5
6
7

int myArray::operator[] (int index) {
    if (index >= X && index <= end) 
	{
		return (array[index - X]);
    }
}
Last edited on
Your subscript operators look different to me.
It's a pity the opening post was edited so much that the original problem has gone AWOL!

My take on the problem, inc the main() function which has gone missing above, is:

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
#include <iostream>
using namespace std;

template <class T>
class myArray
{
private:
    int begin;
    int end;
    T *array;

public:
    myArray(int _begin, int _end)
    {
        begin = _begin;
        end   = _end;
        array = new T[_end - _begin];
        for(int i = 0; i < (_end - _begin); i++)
            array[i] = (T)(_begin+i);
    }
  
    T& operator [](int index)
    {
        static T dummy = T();
        if((begin <= index) && (index < end))
        {
            return array[index - begin];
        }
        return dummy;
    }
};

int main()
{
    myArray<int> myList(2,13);
    myArray<int> yourList(-5,9);
    int i;
    cout << "Elements in myList: ";
    for (i = 2; i < 13; i++)
        cout << myList[i] << " ";
    cout << endl;
    cout << "Elements in yourList: ";
    for (i = -5; i < 9; i++)
        cout << yourList[i] << " ";
    cout << endl;
    system("PAUSE");
    return 0;
}


It is pretty much the same as sun1520's code was before it was re-broken (e.g. the data members and main() have gone?)

But:
- I followed the C++ standard's libray by having "end" one past the end, so I a using < rather than <= for upperbound
- if a bad index is given to operator[], I do not return the first element. Instead I return a static, zero-ed element
- and I think that as far as names go, begin and end are clearer than place and _size.

The results are:

Elements in myList: 2 3 4 5 6 7 8 9 10 11 12
Elements in yourList: -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
Press any key to continue . . .

Andy
Last edited on
Topic archived. No new replies allowed.