0

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

4
  • have you tried myVector.resize(number)? Commented Aug 30, 2012 at 19:59
  • Not the question you asked, but why store the size of the vector in your Test class? Vectors already store their size so if your class has it too you are just storing the same thing twice. Commented Aug 30, 2012 at 20:00
  • Not your question, but I would suggest avoiding having parameters with the exact same name as a member (n in your case). That tends to lead to hard to track down bugs in my experience Commented Aug 30, 2012 at 20:08
  • Yeah, the compiler will sometimes give a warning for member shadowing. I tend to just put an underscore at the end (so I don't accidentally use a reserved identifier) of all my data members. That also allows for having, say, a data member named size_ and a function named size() to retrieve the value in a user-friendly way. Commented Aug 30, 2012 at 20:15

3 Answers 3

3

Add it to your initializers:

Test (int n):
    myVector(n), n(n) {}

Take note that it should be placed before n because the members get initialized in the order they appear in your class. As well, if all your n member does is keep track of the size of the vector, you might as well just use myVector.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, using size_t is your best option because that's the equivalent of what the vector uses for its size (std::vector<T>::size_type).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response... the thing is though, I want to initialize the vector to that size based off the constructor call, not keep track using n. If i were to do myVector(myVector.size()) would't that explode, given I didn't give it an initial size yet?
@PinkElephantsOnParade, I mean get rid of your class's data member n if that's all it does. When the user passes your constructor's argument, just use that to initialize the vector and vector's size() member function afterwards. No extra data member needed.
Ah, I understand now what you meant. Thanks!
0

Use the member initializer list, just like you did for n. Since the vector knows its size, you don't even need to store n. You can just use myVector.size() when you need that.

class Test
{
private:
    std::vector<Object_I_madeup> myVector;
public:
     Test(int n):
       myVector(n) {}
};

Comments

0

Why are you constructing the size separately? The vector can always tell you how many elements it has; storing that information is redundant. You should just initialize it in the initialization list:

class Test {
public:
    Test(size_t size) : myVector(size) { }
private:
    std::vector<Object_I_madeup> myVector;
};

Note that, by using an int, you are artificially limiting the upper bound of the vector.

5 Comments

Still, 2 billion ints is quite a bit of memory... I'd say using an unsigned type for preventing negative sizes would be a more practical use in most situations.
@chris: Sure, but why limit it when there is no need to? The vector is fine with it, so should you be. Good point about negatives as well. The point is, the vector constructor takes a size_t, and this argument is passed directly to it. Why change that? size_t represents the maximum size for a reason.
True, using the same type as the vector, even for that reason, is always a good thing to do. That way every possible value is within range, and it fills the entire range.
@chris: Right. In most cases it is not going to be very large, I just don't see a reason to limit it.
Yeah, I guess my point was that it's worth mentioning the side of using it that's more likely to save work, which is that negative values are impossible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.