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.
#include <iostream>
#include <forward_list>
usingnamespace 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.