• Advertisement
Sign in to follow this  

Size of static array

This topic is 3967 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

I have a program that uses #defines to set the size of the static arrays that it uses. I would like to restrict the scope of the constants used. I tried using static const ints in a class, but it won't let me declare the size of the array with that int. Is there a way to do this that I am missing?

Share this post


Link to post
Share on other sites
Advertisement
class foo
{
private:
static const int NUMBER_OF_ELEMENTS = 6;
int m_Array[NUMBER_OF_ELEMENTS];
};



This should do it.

Share this post


Link to post
Share on other sites
Why don't we turn things around and have the array dictate its size to its users, not the other way around.

int array[100];
for(int i = 0; i < ARRAY_SIZE(array); i++) {}


with
#define ARRAY_SIZE(name) (sizeof(name) / sizeof(name[0]))

Share this post


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

  • Advertisement