tags:

views:

114

answers:

3
+3  A: 

You can make a std::vector<TestCase> allofem; and allofem.push_back(TestCase()) X times; remember to #include <vector> of course. Then you can loop on allofem and compute and then print on each item.

Alex Martelli
To make use of each test case, do I do this? for(int i=0; i<numTestCase; i++) { allofem[i].setRange(); }
Mike
If .setRange is what you want to call on each test case instance, yep, this is workable (not the typical style of loops in C++'s standard library, but it will work fine, so if this is what youre comfortable with, go for it!).
Alex Martelli
I suppose the typical style would be to use an iterator. I'll do that. Thanks
Mike
@medikgt yep: typical style is `for(std::vector<TestCase>::iterator i = allofem.begin(); i != allofem.end(); ++i) i->setRange();` and the like, exactly. But using an int index also works!
Alex Martelli
A: 
for (int i = 0; i < numOfTestCases; ++i) {  
    TestCase t;
    t.setRange();

    t.compute();
    t.print(); 
}
dimba
A: 

In C++ you have two options to create objects: stack or heap.

To create it on the stack you'd run a for loop and declare a variable normally like TestCase t.

The other way is to create it on the heap. This dynamically creates x number of TestCase objects.

TestCase ** tests = new (* TestCase)[x];
for (int i = 0; i < x; i++) {
  tests[i] = new TestCase();
} // for i
eed3si9n
You should be using a `std::vector`.
GMan