1

I'm not exactly sure how to pose this question so I'll start with some example code:

//header file
class A
{
public:
    A();
private:
    int x;
    std::string arr[x];
}

//cpp file

class A
{
public:
    A()
    {
     /*code to get the value of x from a cmd call*/
    }
}

Is this code valid? More specifically, can I have my string array in my header file be of size x, even though x is not specifically given a value until an A object has been created?

If this doesn't work, is my only other option to use a dynamically allocated array?

0

5 Answers 5

5

The code is not valid. You should use a vector instead.

class A
{
public:
    A();
private:
    int x;
    std::vector<std::string> arr;
};

A::A () : x(command_gets_x()), arr(x) {}

Since arr is being initialized by the value of x, the constructor only works when x precedes arr in A (as it is in your definition). However, if the only purpose of x is to track the size of the array, it is not necessary, since a vector has the size() method.

class A
{
public:
    A() : arr(command_gets_x()) {}
    int x () const { return arr.size(); }
    //...
private:
    std::vector<std::string> arr;
};
4
  • 3
    Perhaps a note that this only works when x is declared before arr in the class? Or that we don't need x when we have arr.size().
    – Bo Persson
    Jul 31, 2012 at 23:16
  • Sorry, I suppose this is another question all together, but if I was to make this vector a 2-D array with a predetermined width of 6, would the proper initialization be: std::vector<vector<std::string>> arr(command_get_x(),vector<int>(6,0)); Jul 31, 2012 at 23:51
  • 2
    @user1553248: std::vector<std::vector<std::string> > arr(command_get_x(),std::vector<std::string>(6));
    – jxh
    Jul 31, 2012 at 23:57
  • @user1553248: For bidimensional arrays, I would suggest that you write an adapter class that internally stores a single std::vector<> and provide accessors that map the 2 coordinates into that linear space. The advantages are multiple, from the fact that it is easier to avoid mistakenly creating jagged arrays, to lesser allocations (better memory footprint, better locality)... Aug 1, 2012 at 0:37
3

It's not valid. Array sizes must be constant expressions. Yes, you'll have to use dynamic allocation, though not necessarily directly. You can just use std::vector.

0
2

No, that's not possible, for one C++ doesn't have variable length arrays, and further, the array size must be a compile time constant.

You can in the constructor allocate an array with new, or, better use a std::vector.

1

No, you can't initialize arrays with non-const expressions. This will work, and is close to your original intent:

class A
{
   ...
   const int x = 3;
   std::string arr[x];
};

And in the .cpp file:

int A::x;
0

I found that on my mac, in x-code I could do the following

int x = foo() // get some value for x at runtime
int array[ x ];

but that is seriously uncool!! I just read yesterday that some compilers allow dynamic allocation on the stack, but I would recommend that you stay well clear of that.

If the value of x is not known until runtime, then you cannot allocate an array of size x until runtime. Think about what the compiler does: can an array of size x be allocated if we don't know how big x is? The only remaining option is to allocate at run-time (aka dynamically allocate).

1
  • That seems pretty crazy. Is it compliant with the standard? Aug 1, 2012 at 0:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.