Sorry for two questions in succession.
I would like to declare a vector of an Object as instance data then initialize its size in a constructor, as such:
class Test
{
private:
std::vector<Object_I_madeup> myVector;
int n;
public:
Test(int n):
n(n)
{
myVector(n); //Intending to set the size of the vector to n entries
}
I tried to find a .setSize() a la Java, and doubt I should use resize(). I know this isa simple question – but what is the best way to do this?
Thanks
Add it to your initializers:
Take note that it should be placed before
nbecause the members get initialized in the order they appear in your class. As well, if all yournmember does is keep track of the size of the vector, you might as well just usemyVector.size()instead. You should also be a bit more rigorous with invalid values, as here, someone could pass -5 or something of the sort and you wouldn’t stop them. I realize this is a test, but realize an unsigned parameter would be better. As Ed points out, usingsize_tis your best option because that’s the equivalent of what the vector uses for its size (std::vector<T>::size_type).