Public Group

# Creating an array using a const that is initialized in a class

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone explain to me why this can't/won't work? I'd include the error messages I got but I don't have access to a compiler here. It seems like it should know the size of the array at compile time, at least with the first constructor. The second constructor, I pretty much understand that it could be given variable values, and arrays need to know their size before the program runs. But the first one, it seems like it should work.
class Foo
{
public:
//This is the constructor I really need
Foo(): aConstInt(4) {}
//I'll throw this one in too, but it's bad
Foo(int number): aConstInt(number) {}
private:
//this gets initialized in the constructor, so...
const int aConstInt;
//. . .why doesn't like me declaring the array like this?
int theArray[aConstInt];
};


Thanks a lot!

##### Share on other sites
Try this:

class Foo{  public:	Foo(): iSize(4),theArray(NULL) {theArray=new int[iSize];}	Foo(int number): iSize(number),theArray(NULL)  {theArray=new int[iSize];}	~Foo() {if(theArray!=NULL) {delete [] theArray;}}  private:	const int iSize;	int *theArray;};

Not too safe, but it will get the job done.

##### Share on other sites
Quote:
 Original post by hawflakesCan anyone explain to me why this can't/won't work? I'd include the error messages I got but I don't have access to a compiler here.It seems like it should know the size of the array at compile time, at least with the first constructor. The second constructor, I pretty much understand that it could be given variable values, and arrays need to know their size before the program runs. But the first one, it seems like it should work.

Firstly, because an array must be initialized by a compile-time constant and function arguments are never compile-time with respect to the body of the function. This means that in C++, you can never create an array whose size is determined by a function argument without dynamically allocating it. C99 gets around this by allowing VLAs (Variable Length Arrays) whose sizes are determined at runtime but the data is still allocated on the stack/globally where possible.

Now, even if C++ had VLAs, your code still would not work. Why? Think about it -- what would that imply if you instantiated your class twice using a different size each time? You would have two differently sized instances of the same class. You would not be able to use sizeof on your type (at least not at compile-time, which is how sizeof works in C++), you wouldn't be able to make arrays of your type, or anything else that depends on instances of a class all having the same size.

So now, here's a question for you: do you want to limit the array's size to being initialized by a compile-time constant, or do you want to allow people to provide a size determined at runtime?

If you want the array's size to be determined at run-time, then instead of encapsulating the array, your best bet would be to encapsulate either an ::std::vector instantiation or an ::std::deque instantiation of the appropriate type.

If you truely want the size to be determined at compile-time, which does have many benefits as well, then you need a separate class for each different size. At first that may sound like a tedious task, but it is really not. This is because C++ already has a mechanism for creating different classes based on information known at compile-time. It's called a template, which you probably already know of. If not, a template is pretty much a compile-time function that you use < and > with instead of ( and ) to call. It can "return" either a datatype or a function. Here we will use it to produce types.

#include <cstddef>template< ::std::size_t ArraySize = 4 >class Foo{  int theArray[ArraySize];};foo< 7 > size_7;foo<> size_4;

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633325
• Total Posts
3011372
• ### Who's Online (See full list)

There are no registered users currently online

×