How to make a dynamicly sized container of classes that can be modified.

I want a dynamic container that I can add items to or remove items from and that will allocate memory automatically, unlike an array. But I also want something that I can modify the elements of, and I don't mean replace, but actually run methods and functions of the classes stored in the container. This doesn't work with an array or a list as far as I have tested.

Here is some test 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
#include <iostream>
#include <forward_list>

using namespace std;

class whoKnows
{
    private:
        int hidden;
    public:
        int getHidden();
        void setHidden(int newint);
};

int whoKnows::getHidden()
{
    return hidden;
}

void whoKnows::setHidden(int newint)
{
    hidden = newint;
}

int main()
{
    forward_list<whoKnows> test;
    whoKnows unknown;

    whoKnows twelve;
    twelve.setHidden(12);
    whoKnows thirty;
    thirty.setHidden(30);
    whoKnows six;
    six.setHidden(6);

    test.push_front(twelve);
    test.push_front(thirty);
    test.push_front(six);

    cerr<<"Showing all values.\n";

    for(auto it = test.begin(); it != test.end(); it++)
    {
        unknown = *it;
        cerr<<"Class found, value: "<<unknown.getHidden()<<"\n";
    }

    cerr<<"Increasing all values.\n";

    for(auto it = test.begin(); it != test.end(); it++)
    {
        unknown = *it;
        unknown.setHidden(unknown.getHidden()+1);
    }

    cerr<<"Reshowing all values.\n";

    for(auto it = test.begin(); it != test.end(); it++)
    {
        unknown = *it;
        cerr<<"Class found, value: "<<unknown.getHidden()<<"\n";
    }

    cout<<"Done\n";
    return 0;
}

Ideally the second iteration of values would all be one more than the first time, but they are not.
A forward list would work for me, as I have a program that every frame loops though each object (begin to end) and runs a method which will modify values in that class. As long as I can remove an object at any point in the container, I don't need to access elements randomly, only from begining to end.
Topic archived. No new replies allowed.