Class Array

How would I go about making a class that is an array, sort of like a vector?

Basically, I want it to have members like "MyClass.Add(Params)" to increase the bounds, "foo = MyClass[5]", etc.

Sorry I'm not being very clear, I'm a bit tired right now.
Well, if you are not doing a home assignment, you can simply inherit the std::vector class. Try the following program:

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
#include <iostream>
#include <vector>

template <typename T>
class MyVector : public std::vector<T>
{
	// Bingo. You already have all the properties of a std::vector

	// Now suppose You want to add a new method
	// which prints all the members of the container.
	public:
	void printAll (void)
	{
		typename std::vector<T>::iterator it;

		for (it = this->begin (); it != this->end (); it++)
			std::cout << *it << "\t";

		std::cout << std::endl;
	}
};

int main (void)
{
	MyVector<double> v;

	v.push_back (1.1); // inherited from std::vector
	v.push_back (2.5); // inherited from std::vector
	v.push_back (3.9); // inherited from std::vector

	v.printAll (); // you have defined it yourselves.
}
That helped me get a bit further, but it's still not working properly.

Originally I tried to use a vector of a structure, i.e.

1
2
3
4
5
6
7
8
9
struct MyStruct
{
    int x;
    int y;
    int w;
    int h;
}

vector<MyStruct> *MyVector;


But something like:

1
2
3
4
5
6
7
void add_to_mystruct(int x, int y, etc.)
{
    MyStruct *tmp;
    tmp->x = 5;
    // etc.
    MyVector.push_back(tmp);
}


paired with:

1
2
3
4
5
6
7
int main()
{
    add_to_mystruct(5, 5, 5, 5)
    MyStruct *tmp = MyVector[0];

    cout << tmp->x;
}


Didn't work properly. A debug cout in the add_to_mystruct function told me that the structure was just fine in there.

But the cout in int main() gave me a completely different number.

Any suggestions on how I could replicate the desired behaviour? I chose to try for a class because it seemed a lot more easy to read than a structure vector.
This is a vector with pointers to MyStruct objects:
vector<MyStruct*> MyVector;

What you have created is a pointer to vector of MyStruct (not pointer to MyStruct) objects:
vector<MyStruct> *MyVector;

In your case, this code takes the vector pointed to by MyVector:
MyStruct *tmp = MyVector[0];
, because it is the same as the expression:
MyStruct *tmp = *(MyVector + 0);
and I don't even know how it compiles.

The code never allocates the objects to which the elements point:
MyStruct *tmp;
and consequently the following code writes to arbitrary memory locations:
tmp->x = 5;

You need to allocate the structure like this (if you indeed need vector of pointers):
MyStruct *tmp = new MyStruct();
and then you have to deallocate the objects pointed by the elements of the vector somewhere. Preferably in some destructor.

Regards
Whoops - That's not exactly how it was. It was originally "vector<mystruct*> MyVector;" - But I forgot when I wrote that post.

Thanks - I'll try out fixing the old code now.
Hm. Now I'm getting back to the original issue.

Here's the function to add to the vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void add_to_render_list(const char* filename, int x, int y, int w, int h)
{
    renderlist_entry entry;
    SDL_Surface *tmp_surface = IMG_Load(filename);

    if (tmp_surface == NULL)
    {
        cerr << "Error loading surface from file: " << filename << "\nAbort.";
        return;
    }
    entry.surface = tmp_surface;
    entry.x = x;
    entry.y = y;
    entry.w = w;
    entry.h = h;
    renderlist.push_back(&entry);
}


Seems to work properly.

Now here's the code that makes use of that:

1
2
3
4
5
6
add_to_render_list("grass.png", 0, 0, 100, 100);
    renderlist_entry *tmp_entry = new renderlist_entry();

    for (unsigned int i = 0; i < renderlist.size(); i++)
    {
        tmp_entry = renderlist[i];


It crashes with SIGSEGV there. So what am I doing wrong now? I had it working fine when it was just a vector of SDL_surfaces.
Topic archived. No new replies allowed.