Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


#ActualSyerjchep

Posted 14 June 2013 - 02:46 PM

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:
 
#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.

#1Syerjchep

Posted 13 June 2013 - 02:44 PM

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:
#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.

PARTNERS