0

I have a program that has a custom class that I would like to create many of and access them like an array. Example:

#include <iostream>
using namespace std;

class Thing
{
    public:
             Thing(int value)
             {
                  a = value;
             }
             int doStuff(int diffValue)
             {
                  a = diffValue + a;
             }
    private:
             int a;
};

int main()
{
    Thing things(12)[100];
    things[4].doStuff(12);
    return 0;
}

Obviously that will not work but is there an easy way to do this? I am not too familiar with pointers but if there is an easy way to use them in this case...
Thanks I'm open to any suggestions!

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Agni
0

use vector

#include <vector>
...
int main()
{
    vector<Thing> things;
    Thing oneThing;
    // add OneThing to the vector
    vector.push_back(theThing);

}
0

and you need a default constructor of the class too, since you have declared your own ctor the compiler will not generate it for you...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.