1

Sorry if the title is not clear but I ll explain now my problem. I am new in C++.

I have created a class in C++. Instances of that class are the input of the program and they have to be stored in an array to perform the calculations. The problem is that the number of instances of that class that have to be defined by the user is fixed for a single run but can vary from run to run. Here an example:

#include <<blablah>blahblah>

int main()
{
int number_of_instances = 3;

MyClass first_instance(one_parameter_1, another_parameter_1);
MyClass second_instance(one_parameter_2, another_parameter_2);
MyClass third_instance(one_parameter_3, another_parameter_3);

///////////////////

NOW I HAVE TO STORE ALL THREE IN AN ARRAY LIKE

MyClass array[number_of_instances] = {first_instance, second_instance, third_instance};

THE PROBLEM IS THAT I DO NOT KNOW BEFORE HAND HOW MANY OF THEM ARE THE USER IS GOING TO INPUT

///////////////////

performCalculations(array);
return 0;
}

Thanks a lot in advance.

DShook
  • 14,833
  • 9
  • 45
  • 55
Altober
  • 956
  • 2
  • 14
  • 28
  • 1
    ...Or array and variable-counter. – Ross Apr 14 '11 at 18:44
  • Use `std::vector` and this topic describes few elegant ways to populate your vector : [Implementing Array Initializer](http://stackoverflow.com/questions/5395595/implementing-array-initializer) – Nawaz Apr 14 '11 at 18:49

6 Answers6

6

The typical C++ solution is to use a vector.

vector<MyClass> v;
v.push_back(first_instance);  //add an already instantiated object to the end of the vector
v.push_back(second_instance);
v.push_back(third_instance);

You won't have to worry about memory management and you are able to access the vector like you would a normal array:

v[0].classMember

You can also add items to the vector in a loop if needed like so:

for(int i = 0; i < 5; i++){
    v.push_back( MyClass(i, param2) );
}

And all the objects will be destructed when the vector goes out of scope if you're storing the objects directly in the vector.

One of the downsides to storing the objects directly in the vector is passing the vector as a parameter to a function. This will be a slow operation since the vector (and all the objects it holds) will have to be copied.

DShook
  • 14,833
  • 9
  • 45
  • 55
  • Thanks a lot all of you guys. Ok I understand the concept but now the question is if I do not know how many of them are there going to be. How can I add more instances of v.push_back(number_instance) if I do not know how many of them are there going to be. – Altober Apr 14 '11 at 18:50
  • @Altober - yes, you can just keep calling the push_back function to add more elements – DShook Apr 14 '11 at 18:52
  • @DShook Thanks a lot. Sorry probably I am terrible stupid but I understand that I can write a for loop to keep calling push_back but how would the name of the instances be changed like first_instance, second_instance, third_instance, etc over the different iterations of the for loop. – Altober Apr 14 '11 at 18:57
  • @Altober - check my edit. Keep in mind I'm not at a compiler right now so keep on your toes for errors – DShook Apr 14 '11 at 19:04
  • @DShook: A vector parameter is only copied if it is passed by copy. If it is passed by reference or by pointer, then no copy is made. Even when it is passed by copy, the compiler's optimizer may notice that it is not modified in the function, and will omit making a copy. – Emile Cormier Apr 14 '11 at 19:13
  • @Emile - the default is pass by value where it will be copied, but yes, if you pass by reference it will not be copied. – DShook Apr 14 '11 at 19:16
  • @DShook: Sorry for pointing out what you already know. I was just concerned that your last paragraph might discourage the OP from using vector, because you're not telling the whole story. – Emile Cormier Apr 14 '11 at 19:18
2

If you know the number of instances before you read them all in then you can allocate an array on the heap using new[]. (Don't forget to delete[] them when you've finished.) Note that this requires that the object have a default constructor.

Neil
  • 54,642
  • 8
  • 60
  • 72
  • Sorry Neil what do you mean with read them all?. Thanks for the answer. – Altober Apr 14 '11 at 19:02
  • You said that they are the input of the program, so I was assuming that they needed to be read in somehow, and you might know in advance how many you needed to input. – Neil Apr 14 '11 at 19:05
1

you should use std::vector in this case rather than a built-in array.

#include <vector>
...
std::vector<MyClass> v = {first_instance, second_instance, third_instance};
...
v.push_back(fourth_instance);
Jurlie
  • 1,014
  • 10
  • 27
0

If you don't know how many elements the array will contain, I would use a std::vector instead of a plain array as the vector will grow to accommodate the additional elements.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

What you want is the Vector class from the standard template library, it behaves like an array but it will re-size itself if you fill it's internal allocation. If you do not need random access to it (i.e. use the [] opperator) you may want to use the List class instead. If you use List you will need to create an enumerator to step through it.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

use std::vector<MyClass>, vector template can be found in <vector> header. YOu must learn a little bit to code well, before coding use any of online available c++ FAQs

Riga
  • 2,099
  • 1
  • 19
  • 27