Jump to content
  • Advertisement
Sign in to follow this  
hawflakes

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by hawflakes
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.


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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!