views:

87

answers:

2

I have a problem with setting the size of my array. In my code I have:

class Test {
    public:
       ....//Functions
    private:
      string name[];
};

Test() {
   //heres where i want to declare the size of the array
}

Is this possible?

+2  A: 

You will need to dynamically allocate memory for the array using new.

Declare the variable like this:

private:
    string* name;

And in your constructor do this:

int size = ...
name = new string[size];

And free the memory in the destructor like this:

delete [] name;
Kyle Lutz
+1 Beat me to it.
Jon Purdy
What are some of the advantages of using a dynamic array rather than a vector?
dreamlax
Sorry, I'm new to c++...What's a destructor
poorStudent
@dreamlax: You could use it to write `vector`.
Georg Fritzsche
@John: a destructor is the opposite of the constructor. While the constructor builds a new object, the destructor cleans up after the object is `delete`'d or goes out of scope. In this case you need to free the memory the constructor allocates so that you won't have a memory leak.
Kyle Lutz
hmm, I'll have too look up destructors, but this works thanks alot.
poorStudent
Sorry, Kyle, but I'm the down-voter for the reasons in Dan's answer.
GMan
I'm afraid I'm downvoting as well. It needs to be made clear that when you're doing this, the copy-ctor and dtor need to handle the pointer and the allocated memory correctly. Otherwise, this is a source of severe memory leaks.
greyfade
-1 too, there is no reason to use this in C++, it's more trouble than it's worth.
Matthieu M.
+6  A: 

No. But you could use a vector of strings instead:

private:
  std::vector<std::string> name;

Then in your constructor:

Test()
    : name(sizeOfTheArray)
{
}

The vector will be sized for the number of strings you specify. This means all memory for the strings will be allocated at once. You can change the size of the array as you wish, but there's nothing saying you have to. Thus, you get all the benefits of using a dynamically allocated array, and then some, without the drawbacks.

Dan
Just once, I want to see an array question where someone doesn't recommend `vector` instead without answering the question.
@STingRaySC: take a look at the other answer. Personally I think it's more of a hassle with little benefit.
Dan
I don't disagree, but using an array *is possible too*, and it may be that the OP really wants to know how.
@Stingray: Usually, though, that's not the case. This is C++, we have vector for a reason! Most attempts to use pointers + allocation are misguided attempts. Steer them on the right path. EDIT: In fact, I'm going to down-vote answers that recommend it without mentioning `vector` exists, or that they'll also need to make a copy-constructor and assignment operator as an alternative.
GMan
yea if I was told there wasnt another way other than vectors, then I probably would have used them...thanks for the input.
poorStudent
@John: And why do you want another way other than vectors?
GMan
So, even for a buffer, it is better to use `vector<char>` rather than `new char[someSize]`?
dreamlax
Michael J
I'm pretty sure I have never written `new Anything[size]` in any code, anywhere. I can't think of a case where I would prefer that over `std::vector<Anything>`, unless perhaps if it was absolutely necessary to conform to an existing library.
Dan
@Michael: I assume you meant "There are a few special cases where **a vector** is not as good, but they are rare." Like what? I'm not disagreeing, just curious.
Dan
@Dan: you assume correctly, I typed it the wrong way round. One example where a vector might not be suitable is where a function returns a buffer that might be freed by another function (possibly using a special free function like GlibalFree()) Another is where you need to do fancy things with allocators. Custom container allocators are not for the faint hearted.
Michael J